wp_logout()

Log the current user out.


Source Source

File: wp-includes/pluggable.php

	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 );
	}


Top ↑

Changelog Changelog

Changelog
Version Description
2.5.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Ahir Hemant

    Logout function according user role.

       function wpdocs_redirect_after_logout() {
    
            $current_user   = wp_get_current_user();
            $role_name      = $current_user->roles[0];
    
            if ( 'subscriber' === $role_name ) {
                $redirect_url = site_url();
                wp_safe_redirect( $redirect_url );
                exit;
            } 
    
        }
        add_action( 'wp_logout', 'wpdocs_redirect_after_logout' );
  2. Skip to note 2 content
    Contributed by Codex

    Example

    <?php wp_logout(); ?>
    
    

You must log in before being able to contribute a note or feedback.