Hi, I've added the custom code for auto-login suggested on this topic:
https://toolset.com/forums/topic/automatically-log-in-user-after-registration/
The problem is that my registration form autogenarates the password, the custom code doens't work.
I've tried to get the password by this code:
$user = get_user_by( "email", $_POST['user_email'] );
$signon = array(
'user_login' => $user->user_login,
'user_password' => $user->user_pass,
'remember' => true
);
But it gets something like $P$BGQpGHqt/EzibFIRfJhQy4V8iZGt6m0 instead of uhssntqhEU that is the password I get by the notification email sent by the form.
Is there a way yo autologin a registration form with autogenarate password?
thanks
Hello and thank you for contacting the Toolset support.
You can't really use wp_signon function because you don't have the actual password. Instead you need to log in the user WITHOUT a password. Check this thread it explains how to use wp_clear_auth_cookie, wp_set_current_user , and wp_set_auth_cookie to login the user without password.
https://wordpress.stackexchange.com/a/128445
- https://developer.wordpress.org/reference/functions/wp_clear_auth_cookie/
- https://developer.wordpress.org/reference/functions/wp_set_current_user/
- https://developer.wordpress.org/reference/functions/wp_set_auth_cookie/
My issue is resolved now. Thank you!
This is my final code for a registration form with autogenerate username, password and nickname. The form has only a field for email and sends user login and password by a form notification. I had to use cred_submit_complete instead of cred_save_data or the form doesn't send the notification after autologin:
/**
* Auto log-in new user
*/
add_action( 'cred_submit_complete', 'tssupp_cred_autologin', 10, 2 );
function tssupp_cred_autologin( $post_id, $form_data ){
if ( 180 == $form_data['id'] ) {
if ( !empty( $_POST['user_email'] ) ) {
$user = get_user_by( "email", $_POST['user_email'] );
// Redirect URL //
if ( !is_wp_error( $user ) ) {
wp_clear_auth_cookie();
wp_set_current_user ( $user->ID );
wp_set_auth_cookie ( $user->ID );
$redirect_to = get_site_url();
wp_safe_redirect( $redirect_to );
exit();
}
}
}
}