How to log out everywhere else, destroy all sessions "all other devices"?
WordPress panel service "log out everywhere else" is doing a nice job. I want to use this as a function outside the panel.
Screenshoot
WordPress panel service "log out everywhere else" is doing a nice job. I want to use this as a function outside the panel.
Screenshoot
I know this is an old topic but destroy_all()
will destroy all sessions but destroy_others()
destroys all sessions except the current session
Destroy all sessions:
$sessions = WP_Session_Tokens::get_instance( $user_id );
$sessions->destroy_all();
Destroy all sessions (except current session):
$sessions = WP_Session_Tokens::get_instance( $user_id );
$sessions->destroy_others( wp_get_session_token() );
That button sends an AJAX request that runs wp_ajax_destroy_sessions()
.
It's not really abstracted in such a way that you can re-use it outside of AJAX, but if you copy the source into your own function, minus the JSON parts, then you could perform the same action yourself.
The key part is this bit, which will destroy all sessions for a given user ID:
$sessions = WP_Session_Tokens::get_instance( $user_id );
$sessions->destroy_all();
The rest of the function is just checking the user exists, checking permissions, and sending a JSON response. They might not be relevant for your use case, so the above might suffice.
Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.