Destroy user sessions based on user ID

OK, simple solution after digging in the WordPress code.

// get all sessions for user with ID $user_id
$sessions = WP_Session_Tokens::get_instance($user_id);

// we have got the sessions, destroy them all!
$sessions->destroy_all();

This will log the user with ID $user_id out of WordPress.

Use case: My use case for this is when a user is approved moderation, but then the situation changes and they are declined, they will be ‘kicked’ from the system if they have any active login sessions.

Leave a Comment