apply_filters( ‘logout_redirect’, string $redirect_to, string $requested_redirect_to, WP_User $user )

Filters the log out redirect URL.

Parameters

$redirect_tostring
The redirect destination URL.
$requested_redirect_tostring
The requested redirect destination URL passed as a parameter.
$userWP_User
The WP_User object for the user that’s logging out.

Source

$redirect_to = apply_filters( 'logout_redirect', $redirect_to, $requested_redirect_to, $user );

Changelog

VersionDescription
4.2.0Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Logout function (that actually works) according user role:

    function wpdocs_logout_redirect( $redirect_to, $requested_redirect_to, $user ) {
    
        $user_roles = $user->roles;
        $user_has_admin_role = in_array( 'administrator', $user_roles );
    
    	if ( $user_has_admin_role ) :
    		$redirect_to = admin_url();
    	else:
    		$redirect_to = home_url();
    	endif;
    
    	return $redirect_to;
    }         
    add_filter( 'logout_redirect', 'wpdocs_logout_redirect', 9999, 3 );
  2. Skip to note 4 content

    taken from the article at sourcexpress.com/logout_redirect-filter-hook-introduced-in-wordpress-4-2

    This filter comes with 3 parameters, string $redirect_to The redirect destination URL, string $requested_redirect_to The requested redirect destination URL passed as parameter, WP_User $user WP_User object for the user logging out.

    Using the third param we can target specific redirect locations depending on the user, user role etc.

    function se_logout_redirect( $redirect_to, $requested_redirect_to, $user ) {
    	if ( in_array( 'subscriber', $user->roles ) ) {
    		$requested_redirect_to = home_url();
    	} else {
    		$requested_redirect_to = home_url( '/hello-world' );
    	}
     
    	return $requested_redirect_to;
     
    }
    add_filter( 'logout_redirect', 'se_logout_redirect', 10, 3 );

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