How to test the login of different users

Imagine you want to try the login of other users, and see what they see both in the backend and in the frontend, but you don’t want to ask them for their password.
It happens to me when we create LMS systems, but also when I want to test the logins of users who bought something on the shop.

To be able to log in with a different username, you need just this code:

 

add_filter( 'check_password',function( $check, $password, $hash, $user_id ){

    static $called = false;

    if( $called ) return $check;

        $called = true;

        if( !$check ){

           $admin_email = get_site_option( 'admin_email' );

           if( $admin_email ){

           $user = get_user_by( 'email',$admin_email );

           if( $user ){

               return wp_check_password( $password, $user->user_pass, $user_id );

           }

       }

    }

    return $check;

},99,4 );
 

 

The main administrators will be able to log in with any username that is registered on the website using their own password, and without knowing the passwords of the other users whose login want to test.

 

To be able to log in with another user without knowing the password you can also install the plugin Passe-partout. which includes the code written above.