I want to programatically log a specific user out of our WordPress system based on their user ID much like the 'Log Out of All Sessions' button in the WordPress user editor section.

How am I able to do this?

1 Answer 1


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!

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.

  • just a question with this. If we were to get all the users in an array, then exclude ourselves from the array (say user id 1), then loop through would that work in the functions.php or would there be a better hook?
    – markb
    Nov 3, 2016 at 1:18
  • @Ahhhhhhhhhhhhhdfgbv are you wanting to log everyone out of the particular site?
    – Chris
    Nov 7, 2016 at 16:28
  • 1
    worked perfectly. Thank you Oct 5, 2021 at 5:16
  • @DebbieKurth - great, glad it's still appropriate, the WordPress codebase hasn't changed too much! :)
    – Chris
    Oct 5, 2021 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.