Skip Navigation

[Resolved] Automatically log-in user after registration

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

Problem:
How to automatically log-in users who register with an email address (not username) using a CRED new user form?

Solution:
Custom code is required, and note that the wp_signon() function requires a username, the email address is not sufficient, but can be used within the code to retrieve the username.

/**
 * Auto log-in new user
 */
add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );
function tssupp_cred_autologin( $post_id, $form_data ){
   
    if ( 12519 == $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() );
            }
        }
    }
}

Relevant Documentation:

This support ticket is created 6 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 12 replies, has 5 voices.

Last updated by Nigel 4 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#628178

Hi there,

I'm trying to Automatically log-in users after registration.
I found a function code on the forum, tried to use it but it has no effect.

When I install the code in my functions, the page just loads and the user is still logged out.

I tried to modify the code a bit, since I don't use usernames only e-mail.
Could you guys help me out with this?

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

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

It looks like - you are using wrong ID with if comparison.

Could you please try following code - it should work.

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

Nigel
Supporter

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

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

Hi Christiaan

The edit you made to check the form ID is not valid, the subsequent code will not run.

If 12519 is the ID of your form then you would need to change the following:

// change this...
    if ( 6 == $form_data['12519'] ) {

// to this...
    if ( 12519 == $form_data['id'] ) {
#628294

Hi Minesh and Nigel,

Thanks for the feedback. However te auto-login is still not working.

I placed the CRED registration form on this page: hidden link (this user registration form is ID 12519)

I have set the option to redirect to another page when the user submits the registration.
This redirect leads to: hidden link

But when it redirects, you just see the page with login form. On this page I created a conditional to show login form when the user is not logged in.

You could try yourself to sign-up and see what happens. What am I doing wrong?

#628641

Nigel
Supporter

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

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

Hi Christiaan

The code you are using should log an error if the automatic sign in fails.

Can you check your logs to see if there is a meaningful error reported?

If you haven't already, turn on the debug log by editing your wp-config.php file and change the line with WP_DEBUG like so:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);

That will create a debug log called debug.log in your wp-content directory which you can examine in any text editor. Try signing up with the form again and then inspect the log.

#628645

I just did some tests with the debug log on. What I'veen in the logs are some errors with other plugins (3 total).

All had this error: PHP Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP;

I deactivated those plugins and did a registration/login test again, this didn't work.

What I can do is create an exact duplicate version of my current site on staging domain so you could take a look?

#628647

Nigel
Supporter

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

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

Yes, sure, as I don't want to break anything on your live site.

I will mark your next reply as private so that I can get log-in credentials from you—you may want to create a temporary admin user for me to use that you can later delete.

#628882

Please let me know if you need any information about my website or have problems with something. 🙂

#629044

Nigel
Supporter

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

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

Hi Christiaan

I double-checked with the documentation for wp_signon and you must provide the username, not the email address.

So I rewrote the code to retrieve the username of the new user using the email address and tested it locally and confirmed it worked, so you can replace the original code on your site with this code and you should hopefully find the same.

/**
 * Auto log-in new user
 */
add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );
function tssupp_cred_autologin( $post_id, $form_data ){
  
    if ( 12519 == $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() );
            }
        }
    }
}
#629052

Thanks Nigel, I tried this on my live site and it worked. Thanks for your time and help I really appreciate that.

#1278903

Thanks for that code Nigel, it works perfectly!! I want to save this code somehow, it would be great to have it as functionality within Toolset automatically

#1796341

Nigel, should I assume this code is being added to functions.php? Or does it belong elsewhere? Much Thanks, - Paige

#1796433

Nigel
Supporter

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

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

@paige you could add it to your theme's functions.php file, but a better place would be as a code snippet at Toolset > Settings > Custom Code, so you don't lose it if you swap theme.