public function loginAs(int $user_id, bool $remember = false): bool
    {
        if (!$user_id) {
            return false; // Impossible.
        }
        $on_set_auth_cookie = function (string $value) {
            $_COOKIE[is_ssl() ? SECURE_AUTH_COOKIE : AUTH_COOKIE] = $value;
        };
        $on_set_logged_in_cookie = function (string $value) {
            $_COOKIE[LOGGED_IN_COOKIE] = $value;
        };
        add_action('set_auth_cookie', $on_set_auth_cookie);
        add_action('set_logged_in_cookie', $on_set_logged_in_cookie);

        wp_clear_auth_cookie();
        wp_set_auth_cookie($user_id, $remember);

        remove_action('set_auth_cookie', $on_set_auth_cookie);
        remove_action('set_logged_in_cookie', $on_set_logged_in_cookie);

        wp_set_current_user(0);
        wp_set_current_user($user_id);

        return (int) get_current_user_id() === $user_id;
    }
public function loginAs(int $user_id, bool $remember = false): bool
    {
        if (!$user_id) {
            return false; // Impossible.
        }
        $on_set_auth_cookie = function (string $value) {
            $_COOKIE[is_ssl() ? SECURE_AUTH_COOKIE : AUTH_COOKIE] = $value;
        };
        $on_set_logged_in_cookie = function (string $value) {
            $_COOKIE[LOGGED_IN_COOKIE] = $value;
        };
        add_action('set_auth_cookie', $on_set_auth_cookie);
        add_action('set_logged_in_cookie', $on_set_logged_in_cookie);

        wp_clear_auth_cookie();
        wp_set_auth_cookie($user_id, $remember);

        remove_action('set_auth_cookie', $on_set_auth_cookie);
        remove_action('set_logged_in_cookie', $on_set_logged_in_cookie);

        wp_set_current_user(0);
        wp_set_current_user($user_id);

        return (int) get_current_user_id() === $user_id;
    }
public function loginAs ( int $ user_id , bool $ remember = false ) : bool { if ( ! $ user_id ) { return false ; // Impossible. } $ on_set_auth_cookie = function ( string $ value ) { $ _COOKIE [ is_ssl ( ) ? SECURE_AUTH_COOKIE : AUTH_COOKIE ] = $ value ; } ; $ on_set_logged_in_cookie = function ( string $ value ) { $ _COOKIE [ LOGGED_IN_COOKIE ] = $ value ; } ; add_action ( 'set_auth_cookie' , $ on_set_auth_cookie ) ; add_action ( 'set_logged_in_cookie' , $ on_set_logged_in_cookie ) ; wp_clear_auth_cookie ( ) ; wp_set_auth_cookie ( $ user_id , $ remember ) ; remove_action ( 'set_auth_cookie' , $ on_set_auth_cookie ) ; remove_action ( 'set_logged_in_cookie' , $ on_set_logged_in_cookie ) ; wp_set_current_user ( 0 ) ; wp_set_current_user ( $ user_id ) ; return ( int ) get_current_user_id ( ) === $ user_id ; }
Log in as a specific WP user.

@since 17xxxx WP user utils.

@param int  $user_id  User ID.
@param bool $remember Remember them?

@return bool True if logged-in.
@note This updates cookies in real-time.
Log in as a specific WP user .