Skip Navigation

[Resolved] Automatic login after registration with CRED form

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

Problem:

This issue here is that the user wanted to automatically log in their customers after they have registered for the site.

Solution:

This can be done by using the custom code below.

/**
 * Auto-login new CRED user
 */
add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );
  
function tssupp_cred_autologin( $post_id, $form_data ){
  
    if ( 597 == $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_login'    =>   $_POST['user_email'],
                'user_password' =>   $_POST['user_pass'],
                'remember'      =>   true
            );
            $login = wp_signon( $user, false );
  
            if ( is_wp_error($login) ) {
                error_log( $login->get_error_message() );
            }
  
        }
    }
}

Replace 597 with the ID of your form.

This support ticket is created 5 years, 4 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
- 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 2 replies, has 2 voices.

Last updated by Ian Henderson 5 years, 4 months ago.

Assisted by: Shane.

Author
Posts
#1305885

On my site users register with a CRED form, and are put in a 'pending' user group while awaiting moderator approval.

However, our client wishes for these new users to create a member profile immediately, not on registration (we use a post type 'Members' which allows only one post per user to create member profiles and embellish the user profiles).

The CRED registration form redirects to a page, and ideally I would like to log the users in automatically, and embed the post form for Members on that page. Is there a way to log users in automatically? I looked at these solutions:
https://toolset.com/forums/topic/automatically-log-in-user-after-created/
https://toolset.com/forums/topic/automatic-login-upon-cred-user-form-submission/

but neither of them worked in our site.

#1305897
#1305905

Shane
Supporter

Languages: English (English )

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

Hi Ian,

I managed to fix the code now.

/**
 * Auto-login new CRED user
 */
add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );
 
function tssupp_cred_autologin( $post_id, $form_data ){
 
    if ( 597 == $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_login'    =>   $_POST['user_email'],
                'user_password' =>   $_POST['user_pass'],
                'remember'      =>   true
            );
            $login = wp_signon( $user, false );
 
            if ( is_wp_error($login) ) {
                error_log( $login->get_error_message() );
            }
 
        }
    }
}

The problem was that you were using the email field so I had to change the $_POST['user_login'] to $_POST['user_email'] to get the user email for them to get auto logged in.

Thanks,
Shane

#1305907

Thanks so much Shane! I should have picked that up... working like a charm. best, Ian