Autologin user after registration WordPress

I am trying to autologin a user after registration.

I am trying this in functions.php file:

Read More
    add_action( 'user_register', 'auto_login_user' );
    function auto_login_user($user_id) {
      $user = new WP_User($user_id);
      $user_login_var = $user->user_login;
      $user_email_var = stripslashes($user->user_email);
      $user_pass_var    = $user->user_pass;
      $creds = array();
      $creds['user_login'] = $user_login_var;
      $creds['user_password'] = $user_pass_var;
      $creds['remember'] = true;
      $user = wp_signon( $creds, false );
      if ( is_wp_error($user) )
        echo $user->get_error_message();
        
}

I am getting ERROR:

The password you entered for the username “TheNewUserCreated” is incorrect. Lost your password?

How Can I take the password from the User object?

Also because this is a custom registration process in template registration.php I tried to take it with $_POST and run the function in that file but I didnt have any success that way too…

EDIT:
Ok I am getting the encrypted password, so what is the solution here, how can I autologin the user? Maybe I can do this in registration.php page?

Related posts

Leave a Reply

3 comments

  1. Add below function to functions.php file

     function auto_login_new_user( $user_id ) {
            wp_set_current_user($user_id);
            wp_set_auth_cookie($user_id);
                // You can change home_url() to the specific URL,such as 
            //wp_redirect( 'http://www.wpcoke.com' );
            wp_redirect( home_url() );
            exit;
        }
     add_action( 'user_register', 'auto_login_new_user' );
    
  2. If you are using wp_insert_user(); to register your users then to auto-login them it’s simple.
    This function returns the user ID if success, so use it to login that user.

    $id = wp_insert_user($data);
    //so if the return is not an wp error object then continue with login
    if(!is_wp_error($id)){
        wp_set_current_user($id); // set the current wp user
        wp_set_auth_cookie($id); // start the cookie for the current registered user
    }
    

    but to follow what you have already it can be like this:

    add_action( 'user_register', 'auto_login_user' );
    function auto_login_user($user_id) {
        wp_set_current_user($user_id); // set the current wp user
        wp_set_auth_cookie($user_id); // start the cookie for the current registered user
    }
    //this code is a bit tricky, if you are admin and you want to create a user then your admin session will be replaced with the new user you created :)
    
  3. function auto_login() {
                $getuserdata=get_user_by('login',$_GET['login']);
                $tuserid=$getuserdata->ID;
                $user_id = $tuserid;
                $user = get_user_by( 'id', $user_id );
                if( $user ) {
                    wp_set_current_user( $user_id, $user->user_login );
                    wp_set_auth_cookie( $user_id );
                    do_action( 'wp_login', $user->user_login );
    
                }
            }
            add_action('init', 'auto_login');
    

    i had have the same problem as your and found this best solution. try this in your functions.php file. and one thing more use submit your reset form in a function in functions.php file