logout_redirect
This filter is documented in wp-login.php */ Filters the URL where a user is redirected after logging out.
add_filter( 'logout_redirect', $callback, 10, 2 );
Description
Fires after a user logs out, allowing developers to customize the redirect URL. Modify the `$redirect_to` parameter to send users elsewhere upon logout. This hook is crucial for controlling post-logout navigation.
Usage
add_filter( 'logout_redirect', 'your_function_name', 10, 2 );
Parameters
-
$redirect_to(mixed) - - **$requested_redirect_to** `mixed`
-
$old_user(mixed)
Return Value
The filtered value.
Examples
<?php
/**
* Custom redirect after logout for specific user roles.
*
* If a user with the 'administrator' role logs out, redirect them to the WordPress dashboard.
* Otherwise, let WordPress handle the default logout redirect.
*
* @param string $redirect_to The URL to redirect to after logout.
* @param string $requested_redirect_to The URL the user initially requested to redirect to.
* @param WP_User $old_user The user object of the user who logged out.
* @return string The modified redirect URL.
*/
add_filter( 'logout_redirect', function( $redirect_to, $requested_redirect_to, $old_user ) {
// Check if the logged-out user has the 'administrator' role.
if ( isset( $old_user->roles ) && is_array( $old_user->roles ) && in_array( 'administrator', $old_user->roles ) ) {
// If they are an administrator, redirect them to the admin dashboard.
return admin_url();
}
// For all other users, return the original redirect URL.
return $redirect_to;
}, 10, 3 );
?>
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/includes/user-switching.php:375
}
}
}
}
if ( ! $new_user ) {
/** This filter is documented in wp-login.php */
$redirect_to = apply_filters( 'logout_redirect', $redirect_to, $requested_redirect_to, $old_user );
} else {
/** This filter is documented in wp-login.php */
$redirect_to = apply_filters( 'login_redirect', $redirect_to, $requested_redirect_to, $new_user );
}
/**
* Filters the redirect location after a user switches to another account or switches off.