The logout_redirect WordPress PHP Filter allows you to modify the URL a user is redirected to after they log out.
Usage
add_filter('logout_redirect', 'my_custom_logout_redirect', 10, 3); function my_custom_logout_redirect($redirect_to, $requested_redirect_to, $user) { // your custom code here return $redirect_to; }
Parameters
- $redirect_to (string): The redirect destination URL.
- $requested_redirect_to (string): The requested redirect destination URL passed as a parameter.
- $user (WP_User): The WP_User object for the user that’s logging out.
More information
See WordPress Developer Resources: logout_redirect
Examples
Redirect to home page
Redirect users to the home page after logging out:
add_filter('logout_redirect', 'redirect_to_home', 10, 3); function redirect_to_home($redirect_to, $requested_redirect_to, $user) { return home_url(); }
Redirect to custom URL
Redirect users to a custom URL after logging out:
add_filter('logout_redirect', 'redirect_to_custom_url', 10, 3); function redirect_to_custom_url($redirect_to, $requested_redirect_to, $user) { return 'https://example.com/your-url/'; }
Redirect based on user role
Redirect users to different pages based on their role after logging out:
add_filter('logout_redirect', 'redirect_by_user_role', 10, 3); function redirect_by_user_role($redirect_to, $requested_redirect_to, $user) { if (in_array('administrator', $user->roles)) { return admin_url(); } else { return home_url(); } }
Redirect to the same page
Redirect users to the same page they logged out from:
add_filter('logout_redirect', 'redirect_to_same_page', 10, 3); function redirect_to_same_page($redirect_to, $requested_redirect_to, $user) { return $_SERVER['HTTP_REFERER']; }
Use requested redirect if available
Use the requested redirect URL if it’s provided, otherwise use the default:
add_filter('logout_redirect', 'use_requested_redirect', 10, 3); function use_requested_redirect($redirect_to, $requested_redirect_to, $user) { if (!empty($requested_redirect_to)) { return $requested_redirect_to; } return $redirect_to; }