Skip Navigation

[Resolved] Autologin after registration with autogenarate password

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

Problem:
The user is user a form to register new users, he would like to automatically login the user after registration.

Solution:
This will need custom code to be implemented. It will 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.

Check the user's custom code here https://toolset.com/forums/topic/autologin-after-registration-with-autogenarate-password/#post-1960459

Relevant Documentation:

This support ticket is created 3 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 2 replies, has 2 voices.

Last updated by umbertoZ 3 years, 9 months ago.

Assisted by: Jamal.

Author
Posts
#1958583

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

#1959973

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/

#1960459

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();
      }

    }
  }
}