"Log Out Everyone" Button for Emergencies
Adds a button under Tools > Log Out Everyone that ends every login session on the site except your own, useful after a suspected break-in or a staff change.
Opens WPIDE on
Code
add_action( 'admin_menu', function () {
add_management_page( 'Log Out Everyone', 'Log Out Everyone', 'manage_options', 'wpide-logout-all', function () {
if ( isset( $_POST['wpide_logout_all'] ) && check_admin_referer( 'wpide_logout_all' ) && current_user_can( 'manage_options' ) ) {
$count = 0;
$ids = get_users( array( 'meta_key' => 'session_tokens', 'fields' => 'ID', 'number' => -1 ) );
foreach ( $ids as $id ) {
$manager = WP_Session_Tokens::get_instance( (int) $id );
if ( get_current_user_id() === (int) $id ) {
$manager->destroy_others( wp_get_session_token() );
} else {
$manager->destroy_all();
$count++;
}
}
printf( '<div class="notice notice-success"><p>%s</p></div>', esc_html( sprintf( 'Logged out %d user(s) and your other sessions.', $count ) ) );
}
echo '<div class="wrap"><h1>Log Out Everyone</h1><p>End all login sessions on this site except the one you are using now.</p>';
echo '<form method="post" onsubmit="return confirm(\'Log out every user now?\');">';
wp_nonce_field( 'wpide_logout_all' );
submit_button( 'Log out everyone', 'primary', 'wpide_logout_all' );
echo '</form></div>';
} );
} );
Notes
Users only need to log in again, so also change passwords if you suspect they were stolen.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.