How to Destroy a User Session in WordPress

Below is a PHP function that can programmatically logout or destroy a user session in WordPress taking the user ID as an argument.


/**
 * Destroy the session of a user.
 *
 * @param int $user_id
 */
function pp_destroy_user_session( $user_id ) {
	// 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();
}
Don’t miss out!
Subscribe to My Newsletter
Invalid email address