Skip to content

Instantly share code, notes, and snippets.

@siaeb
Created August 21, 2022 06:22
Show Gist options
  • Save siaeb/37f3f90570b9a4b883f7cb3ccd3c9009 to your computer and use it in GitHub Desktop.
Save siaeb/37f3f90570b9a4b883f7cb3ccd3c9009 to your computer and use it in GitHub Desktop.
WordPress force user logout - destroy all user sessions
add_action('admin_init', 'wpt_force_logout', 15);
function wpt_force_logout() {
$action = isset($_GET['action']) ? strtolower($_GET['action']) : false;
if (!$action || 'wpt-logout' !== $action) {
return;
}
$user_id = isset($_GET['user_id']) ? absint($_GET['user_id']) : false;
if (!$user_id || empty(trim($user_id))) {
return;
}
// Just admins can do this operation
if (!current_user_can('manage_options')) {
return;
}
$user = get_user_by('id', $user_id);
if (!$user) {
return;
}
// 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();
wp_safe_redirect(admin_url());
exit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment