User Registration hook to log user in no longer functions [RESOLVED]

We’ve got a pretty complicated purchase form for virtual goods. Users can buy a tree in a forest, with a donation.

It’s got several moving parts.

  • it’s using an older version of the Stripe plugin to process payments
  • it’s using the User Registration plugin to create a user, if the submitter ticks a box
  • there’s custom code to generate tree Custom Post Types (the purchased trees are real trees, with lat & long so they can be plotted on a map)
  • if the user has opted to join, then the newly minted tree has its post author set to the new user (or the current logged in user if the user is already logged in)

After a successful purchase, newly created users are auto-logged in, and redirected to a page that shows a summary of their purchases (ie tree posts that belong to the current logged in user.

This is complicated, and it is handled by a custom class that I wrote.

It has worked fine for 4+ years. I’ve got a ton of logs that show users being made, trees being made, and assigned to users.

Sometime after March 30, plugin updates were run, and the custom code broke.

Here’s the hook that previously successfully logged users in:

	/**
	 * Auto login after registration - only required if the User Reg feed has fired (ie user is not logged in).
	 */
	function gravity_registration_autologin($user_id, $user_config, $entry, $password)
	{
		$user = get_userdata($user_id);
		$user_login = $user->user_login;
		$user_password = $password;
		if ($this->debug) write_log("/*USER REG IS RUNNING */");
		if ($this->debug) write_log($user);
		wp_signon(array(
			'user_login' => $user_login,
			'user_password' =>  $user_password,
			'remember' => false
		));

		// cache this and use it later to assign trees to users
		$this->saved_user_id = $user_id;
	}

It’s registered with:
add_action('gform_user_registered', array($this, 'gravity_registration_autologin'), 10, 4);

It runs, the new user is found, and the result of wp_signon() is, as expected, a user object. But the user is not logged in. This used to work fine.

Also, in a subsequent hook when the trees are made, the value of $this->saved_user_id is now undefined, which results in the trees not being assigned to the user.

That hook (where the trees are made and assigned) is set up with
add_action('gform_after_submission_' . $form_id, array($this, 'tree_purchase_make_some_trees'), 10, 2);

The hooks are running in the correct order, according to my log.

I’m lost how the value of a class variable, over the course of a request, can become undefined. The class is a singleton, 100%, and there’s nothing in the logs to suggest it is being re-instantiated.

I’ve been along time GF user (I have a Developer Licence) but I don’t do much WP dev these days. I’ve been asked to help with this site, since I developed it, but I’m stumped.

To improve form submission performance, add-on feed processing has been moved to the background by default. This means the feeds are processed in a separate request, where the user isn’t present.

Auto login isn’t compatible with background feed processing because the current user object and auth cookie are set in the request where the user isn’t present.

You can use the following filter to disable background feed processing for the add-on, so the feed will be processed in the same request as the form submission: https://docs.gravityforms.com/gform_is_feed_asynchronous/#disable-for-user-registration-auto-login

Thanks so much for the insight. All signs pointed to multiple requests, but I was never going to unpick it myself.

Site is fixed.

Great to have a forum again, I remember gravityhelp.com . Having a single place where people interact (and devs contribute!) is a big plus.

1 Like