wp_logout()
Logs the current user out.
Source
File: wp-includes/pluggable.php
.
View all references
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 );
}
Hooks
-
do_action( 'wp_logout',
int $user_id ) -
Fires after a user is logged out.
Changelog
Version | Description |
---|---|
2.5.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
Logout function according user role.
Top ↑
Feedback
The WP_User object would have been destroyed at this point in the logout life-cycle. Therefore a redirect based on user-role is not feasible.
object(WP_User)[947] public 'data' => object(stdClass)[948] public 'ID' => int 0 public 'caps' => array (size=0) empty public 'cap_key' => null public 'roles' => array (size=0) empty public 'allcaps' => array (size=0) empty public 'filter' => null private 'site_id' => int 0
— By terungwa —wp_get_current_user function does not return the logged out user — By nimam2 —
Example
Top ↑
Feedback
Warning: This function logs the user out as soon as it is called. Don’t add it to button values or widgets using a plugin to insert PHP. If you do so, you might lose access to your admin because you will be logged out every time you visit a page. If you want to create a Logout button, or a link the user can click to log out, use wp_logout_url() instead. — By Carike —
Please do not copy / paste this code into a button if you are a new user.
wp_logout();
should always be wrapped in an if statement. — By Carike —