Skip Navigation

[Resolved] Auto-login doesn’t work

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to automatically log in users when they register using a Toolset User Form?

Solution:
The client uses code to auto-login users but it is not working: https://toolset.com/forums/topic/auto-login-doesnt-work-2/#post-1268705

This code expects the password fields to be included in the form, but the client is auto-generating the passwords.

An updated version of the code for this scenario is here: https://toolset.com/forums/topic/auto-login-doesnt-work-2/#post-1269167

It should be possible to add a little logic so that either scenario is covered by a single code snippet.

This support ticket is created 5 years, 7 months 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 5 replies, has 2 voices.

Last updated by StanleyT8485 5 years, 6 months ago.

Assisted by: Nigel.

Author
Posts
#1268705

I am trying to: Auto-login our users after they submit our CRED form.

I used this code which was a solution:

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

I replaced 6 with our form id 1908

Link to a page where the issue can be seen: Localhost

However, after the form submission the users aren't logged in.

I am also using all the form fields by default - hidden link
Note: I am auto generating the username and password.

Stan

#1268985

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The above code doesn't work because you are auto-generating the password and so the password isn't available to the code to log in with.

It's possible to retrieve the hashed password for the user, but that can't be used to log in, you need the actual password.

I need to look into this further to see what's possible and I'll get back to you.

But if you include the password fields rather than auto-generate the password (you had another question about that) then the above code should work.

#1269167

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I updated the code for the scenario where the password is auto-generated, which should now work if you want to try it:

/**
 * Auto-login new CRED user where password auto-generated
 */
add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );
function tssupp_cred_autologin( $post_id, $form_data ){
  
    if ( 1908 == $form_data['id'] ) { // Edit as required

        if ( !empty( $_POST['user_email'] ) ) {

            // Get new user based on email
            $user = get_user_by( 'email', $_POST['user_email'] );

            // Make a new password an update user with it
            $new_pass = wp_generate_password( 10, false );
            $user->data->user_pass = $new_pass;
            $updated = wp_update_user( $user );
            if ( is_wp_error($updated) ) {
                error_log( $updated->get_error_message() );
            }            

            // Log in with new password
            $user_data = array(
                'user_login'    => $user->data->user_login,
                'user_password' => $user->data->user_pass,
                'remember'      => true
            );
          $login = wp_signon( $user_data, false );
  
            if ( is_wp_error($login) ) {
                error_log( $login->get_error_message() );
            }
  
        }
    }
}
#1270133

Thanks for this, Nigel.

We want to set the password for the user and all the password will be the same. The default password for all our users is [edit].

I tried this but it gave me the error message. However, the user was created.

add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );
 
function tssupp_cred_autologin( $post_id, $form_data ){
 
    if ( 1908 == $form_data['id'] ) {
 
        if ( !empty( $_POST['user_email'] ) ) {

        	// Get new user based on email
        	$user = get_user_by( 'email', $_POST['user_email'] );

        	// Login with new password
        	$user_data = array(
        		'user_login' => $user->data->user_login,
        		'user_password' => $user->data->user_pass, // I tried replacing this with required password but it doesn't work too
        		'remember' => true
        	);
        	
        $login = wp_signon( $user, false );
 
            if ( is_wp_error( $login ) ) {
                error_log( $login->get_error_message() );
            }
        }
    }
}
#1270367

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I thought the form settings were that the password is auto-generated?

That means WordPress will create a random password.

If you want all the users to have the same password of your choice then you should not specify that the password is auto-generated. The form submission needs to include the password fields to be able to successfully create the user. You could hide them with CSS and populate them with JS, or it may be possible to omit them from the form and add them using an API filter.

But, probably the easiest solution would be to persist with auto-generating the password together with the code that I shared in my last reply, and simply replace the new random password with your chosen password, i.e. change the line $new_pass = wp_generate_password( 10, false ); to $new_pass = 'chosen password';

#1271195

My issue is resolved now. Thank you!