Allow one active login per account. Logging in on a new device ends the sessions on every other device.
add_action( 'set_logged_in_cookie', function ( string $cookie, int $expire, int $expiration, int $user_id, string $scheme, string $token ): void {
if ( '' === $token ) {
return;
}
WP_Session_Tokens::get_instance( $user_id )->destroy_others( $token );
}, 10, 6 );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
On paid membership sites, one account is sometimes shared by several people. Allowing only one session at a time stops that. It’s also a simple security measure: a new login always ends any older session someone else may have.
WordPress gives every login a session token, stored with the user. When the login cookie is set, the set_logged_in_cookie action receives the new token. destroy_others() then removes every other token for that user, so other browsers are logged out on their next page load.
user_can( $user_id, 'manage_options' ).Log in on one browser, then log in as the same user in a private window. Reload the first browser: you should be logged out.