wp_logout()WP 2.5.0

Log the current user out.

Pluggable function — this function can be replaced from a plugin. It means that this function is defined (works) only after all plugins are loaded (included), but before this moment this function has not defined. Therefore, you cannot call this and all functions depended on this function directly from a plugin code. They need to be called on plugins_loaded hook or later, for example on init hook.

Function replacement (override) — in must-use or regular plugin you can create a function with the same name, then it will replace this function.

Hooks from the function

Return

null. Nothing (null).

Usage

wp_logout();

Examples

0

#1 Organizing WordPress exit by event

Suppose we need to logout the system if the query parameter logout is specified in the address bar:

if( isset( $_GET['logout'] ) ){
	wp_logout();
}

Never use it separately from the IF statement!

Do not use such code in a button or link - wp_logout() should always be wrapped in an if statement. Otherwise the user will simply be logged out automatically.

wp_logout()

Changelog

Since 2.5.0 Introduced.

wp_logout() code WP 6.5.2

function wp_logout() {
	$user_id = get_current_user_id();

	wp_destroy_current_session();
	wp_clear_auth_cookie();
	wp_set_current_user( 0 );

	/**
	 * Fires after a user is logged out.
	 *
	 * @since 1.5.0
	 * @since 5.5.0 Added the `$user_id` parameter.
	 *
	 * @param int $user_id ID of the user that was logged out.
	 */
	do_action( 'wp_logout', $user_id );
}