Skip Navigation

[Resolved] Auto login after user registration

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

Last updated by katjaL 4 years, 8 months ago.

Assisted by: Shane.

Author
Posts
#1538825

I want the user be logged in automatically after filling registration form. I use this code below I found in one thread. The form id is 12788. But the user is not logged in. I tried ajax, no help. The username and nickname are auto-generated. Does that have some bearing on this or is this code somehow wrong?

/**
* 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( $_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() );
}

}
}
}

#1538979

Shane
Supporter

Languages: English (English )

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

Hi Katja,

Thank you for getting in touch.

Actually yes it does have some bearing on this because the code is searching for the user_login field.

If this is autogenerated then it won't be on the form.

Could you send me a screenshot of the First and Last name of a user field as well as what the autogenerated login is.

I should be able to modify the code to work for this case.

Thanks,
Shane

#1538987
Screenshot 2020-03-04 at 16.11.05.png

Hi Shane, thank you. Sorry I don't understand what you want - the first and last name are the default first and last name user fields. I attach here the user form screenshot, though this probably isn't what you wanted 🙂
But let me know and I will send the right one.

#1539011

Shane
Supporter

Languages: English (English )

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

Hi Katja,

Actually no this isn't it. I want a screenshot of the generated user profile. The first and the last name.

I want to confirm that the auto generated login is from first and last name of the user.

Thanks,
Shane

#1539029
Screenshot 2020-03-04 at 16.32.56.png

🙂 still in dark here, you want this?

#1539049

Shane
Supporter

Languages: English (English )

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

Screenshot 2020-03-04 at 9.38.44 AM.png

Hi Katja,

Yes this is what I mean but I need to see the username that is generated as well.

Here is an example. See Screenshot.

Thanks,
Shane

#1539063
Screenshot 2020-03-04 at 16.50.34.png

Here you go.

#1539073

so the username is generated from email start.

#1539135

Shane
Supporter

Languages: English (English )

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

Hi Katja,

Try 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 ( 12788 == $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' => substr( $_POST['user_email'], 0, strpos( $_POST['user_admin'], "@")),
'user_password' => $_POST['user_pass'],
'remember' => true
);
$login = wp_signon( $user, false );

if ( is_wp_error($login) ) {
error_log( $login->get_error_message() );
}

}
}
}

Try this and let me know if it helps.

Thanks,
Shane

#1539139

Thank you Shane. Unfortunately it doesn't help.

#1539151

Shane
Supporter

Languages: English (English )

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

Hi Katja,

I forgot to make one change.

Here is the refined code.

/**
* 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( $_POST['user_email'] ) && !empty( $_POST['user_pass'] ) ) {
 
// get the user credentials from the $_POST object
$user = array(
'user_login' => substr( $_POST['user_email'], 0, strpos( $_POST['user_admin'], "@")),
'user_password' => $_POST['user_pass'],
'remember' => true
);
$login = wp_signon( $user, false );
 
if ( is_wp_error($login) ) {
error_log( $login->get_error_message() );
}
 
}
}
}
#1539169

Sorry, still no help.

#1539215

Shane
Supporter

Languages: English (English )

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

Hi Katja,

Would you mind allowing me to have admin access to the website to check on this for you ?

The private fields will be enabled for your next response.

Thanks,
Shane

#1540879

Shane
Supporter

Languages: English (English )

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

Hi Katja,

I tried checking on this with credentials that you've sent but i'm getting error 404 on the link that you've sent. See Screenshot

Could you check on this for me .

Thanks,
Shane

#1541261

Hi Shane, please delete the screenshot or hide it. The login address has changed too now that the site is published and the domain changed. The new login address is in edited private message.