Autologin after registration for specific form

Hi
I use the following code to autologin after user registration using the Gravity registration add-on. (documentation)

add_action( 'gform_user_registered', 'wpc_gravity_registration_autologin',  10, 4 );
/**
 Auto login after registration.
 */ 
function wpc_gravity_registration_autologin( $user_id, $user_config, $entry, $password ) {
$user = get_userdata( $user_id );
$user_login = $user->user_login;
$user_password = $password;

wp_signon( array(
	'user_login' => $user_login,
	'user_password' =>  $user_password,
	'remember' => true

) );
}

I have 2 other forms that I use for registration purposes, but I don’t want to autologin after registering users through these forms.

Is there any way to restrict autologin to a specific form’s ID?

If you click the user registration feed and scroll to the bottom of the page, do you not see this option? If you do all you need to do is toggle it.

If you don’t GP auto login is a plugin by Gravity Wiz that can enable this functionality for you.

2 Likes

This will make it specific to the form the user is registering.

If that doesn’t help then hopefully someone can help you with the PHP code to add the formid.

1 Like

Thank you Dere (@user5c0166073a6c48.6 ) for your complete answer.

Furtunetally, after a bunch of other research, I found the answer in this post.

To autologin just for a specific form, we need to add the following code after the function’s start (Suppose form ID is 5):

if(5 == $entry['form_id']) {

//codes go here 
}

Complete code looks like this:

add_action( 'gform_user_registered', 'wpc_gravity_registration_autologin',  10, 4 );
/**
 Auto login after registration.
 */ 
function wpc_gravity_registration_autologin( $user_id, $user_config, $entry, $password ) {
if(5 == $entry['form_id']) {
$user = get_userdata( $user_id );
$user_login = $user->user_login;
$user_password = $password;

wp_signon( array(
'user_login' => $user_login,
'user_password' =>  $user_password,
'remember' => true

) );
}
}

You can change the ID to your form’s ID.

1 Like