wordpress programmatically logout everywhere else

How can I logout from every where else this place in wordpress? [not wp_logout() because it destroys current session only]

I used this function but it did not work:

WP_User_Meta_Session_Tokens::destroy_other_sessions();

Related posts

Leave a Reply

2 comments

  1. You must first get the user id, then get the token, protect it and destroy the other sessions. Here’s a working example:

    global $wp_session;
    $user_id = get_current_user_id();
    $session = wp_get_session_token();
    $sessions = WP_Session_Tokens::get_instance($user_id);
    $sessions->destroy_others($session);
    
  2. Here is a working version hooked to the init action.

    /**
     * Destroys all sessions for this user except the one with the given token (presumably the one in use).
     */
    add_action( 'init', 'destroy_all_other_current_user_sessions' );
    
    function destroy_all_other_current_user_sessions() {
    
        $manager = WP_Session_Tokens::get_instance( get_current_user_id() );
    
        $manager->destroy_others( wp_get_session_token() );
    
    };