Created
August 21, 2022 06:22
-
-
Save siaeb/37f3f90570b9a4b883f7cb3ccd3c9009 to your computer and use it in GitHub Desktop.
WordPress force user logout - destroy all user sessions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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