Skip Navigation

[Resolved] Autologin after user registration not working

This support ticket is created 4 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 14 replies, has 2 voices.

Last updated by Shane 4 years ago.

Assisted by: Shane.

Author
Posts
#1561769

Tell us what you are trying to do?

I am trying to automatically login a user on completion of registration.

Is there any documentation that you are following?

https://toolset.com/forums/topic/auto-log-in-after-sign-up-with-user-form/

Is there a similar example that we can see?

https://toolset.com/forums/topic/auto-log-in-after-sign-up-with-user-form/

What is the link to your site?

hidden link

#1561903

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Peter,

Thank you for getting in touch.

I suspect the issue here is that your Fields on your form is different.

Could you send me a screenshot of the user form so that I can see what fields you are using?

Thanks,
Shane

#1561935
screencapture-bankher-br-wp-admin-post-php-2020-03-24-17_13_29.jpg

Here you go. I have also tried with a manual password field. The user is being created and the form is redirecting to the correct page, but not with the new user in a logged in state.

#1561945

This is the snippet I am running:

/**
 * New custom code snippet.
 */
 
toolset_snippet_security_check() or die( 'Direct access is not allowed' );

/**
 * Auto log-in new user
 */
add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );
function tssupp_cred_autologin( $post_id, $form_data ){
    
    if ( 80 == $form_data['id'] ) {
    
        if ( !empty( $_POST['user_email'] ) && !empty( $_POST['user_pass'] ) ) {
    
            $user = get_user_by( "email", $_POST['user_email'] );
  
            $signon = array(
                'user_login'    => $user->user_login,
                'user_password' => $_POST['user_pass'],
                'remember'      => true
            );
  
            $login = wp_signon( $signon, false );
    
            if ( is_wp_error($login) ) {
                error_log( $login->get_error_message() );
            }
        }
    }
}
#1561967

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Peter,

Thank you for sharing this.

While what you are doing is correct for the most part, this won't work.

Your user's password is being auto generated. For this to work the user must set their password manually.

Given that the user's password is required for them to log in and we have no way of getting this using the code, except directly from the form.

You must adjust your form so that it gets the user enters their desired password.

Thanks,
Shane

#1561969

Thank you. That makes perfect sense. However, I have also tried this with a manual password field in place, but the result was the same. The user was registered and correctly redirected, but not in a logged in state. You can test here: hidden link

In that case, the form looks like this:

[creduserform]
	[cred_field field='form_messages' class='alert alert-warning']
	<div class="form-group">
		<label>E-mail</label>
		[cred_field field='user_email' class='form-control' output='bootstrap']
	</div>
	<div class="form-group">
		<label>Set password</label>
        [cred_field field='user_pass' class='form-control' output='bootstrap']
	</div>
    <p>
      <label>
		<input type="checkbox" name="mc4wp-subscribe" value="1" checked />
		Subscribe to our newsletter.
      </label>
    </p>
	[cred_field field='form_submit' output='bootstrap' value='Enviar' class='btn btn-primary btn-lg']
[/creduserform]
#1562033

I have not got this working but I have changed the architecture of my journey. I am now using the mailchimp sign-up form, pre-populating the email field on my Toolset registration form as a second step via a url query.

This has allowed me to acheive my goal, perhaps slightly more elegantly, too.

Thank you for your help.

#1562789

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Peter,

Happy to see that you were able to continue working on your site. I also must apologize for the delay in response. My shift had ended when the response was submitted.

However should you require this in the future the modified code below should get it working for you.

/**
* Auto-login new CRED user
*/
add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );
   
function tssupp_cred_autologin( $post_id, $form_data ){
   
if ( 12788 == $form_data['id'] ) { // Edit as required
if ( !empty( $_REQUEST['user_email'] ) && !empty( $_REQUEST['user_pass'] ) ) {
   
// get the user credentials from the $_POST object
$user = array(
'user_login' => substr( $_REQUEST['user_email'], 0, strpos( $_REQUEST['user_email'], "@")),
'user_password' => $_REQUEST['user_pass'],
'remember' => true
);
$login = wp_signon( $user, false );
   
if ( is_wp_error($login) ) {
error_log( $login->get_error_message() );
}
   
}
}
}

Just replace the 12788 with the id of your form and this should login the user after they register.

Thanks,
Shane

#1569799

Hi Shane. I've reverted to this now.

Still can't get it working with the solution you have provided. I have an email and password field and am using your code with the form ID changed. It is successfully registering users but still failing to log them in.

Please can you assist? Perhaps you would like to activate hidden fields here, so that I may give you access.

#1569965

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Peter,

Yes please provide me with admin access to the website as well as a link to the form where I can test out the code.

The private field will be enabled for your next response.

Thanks,
Shane

#1569983

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Peter,

Is this site public ? As the server ip address couldn't be found.

If its on a private please provide the host information so that I can add it to my host file.

The private fields will be enabled once more.

Thanks,
Shane

#1570015

Of course. Sorry.

#1570929

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Screenshot 2020-04-01 at 10.29.33 AM.png

Hi Peter,

Thank you for the host file information however it seems I need another set of credentials. See Screenshot.

Could you provide these as well.

Thanks,
Shane

#1571113

I did include these in the FTP fields in the earlier response.

#1571163

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Peter,

So from what I see the code works fine. The problem was with your form. It was still autogenerating the user's password.

I've remade the form and set it not to autogenerate the passwords. Your users should now be automatically logged in.

Thanks,
Shane

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.