The logout_url WordPress PHP Filter allows you to modify the logout URL before it is output on your site.
Usage
add_filter('logout_url', 'your_function_name', 10, 2);
function your_function_name($logout_url, $redirect) {
// Your custom code here
return $logout_url;
}
Parameters
- $logout_url (string) – The HTML-encoded logout URL.
- $redirect (string) – Path to redirect to on logout.
More information
See WordPress Developer Resources: logout_url
Examples
Custom Redirect After Logout
Modify the logout URL to redirect users to the homepage after logging out.
add_filter('logout_url', 'custom_logout_redirect', 10, 2);
function custom_logout_redirect($logout_url, $redirect) {
// Redirect to homepage
$redirect = home_url('/');
$logout_url = add_query_arg('redirect_to', urlencode($redirect), $logout_url);
return $logout_url;
}
Append Custom Query Parameter
Add a custom query parameter to the logout URL.
add_filter('logout_url', 'append_custom_query_parameter', 10, 2);
function append_custom_query_parameter($logout_url, $redirect) {
// Add custom query parameter
$logout_url = add_query_arg('custom_param', 'value', $logout_url);
return $logout_url;
}
Change Logout URL for a Custom Logout Page
Change the logout URL to a custom logout page.
add_filter('logout_url', 'change_to_custom_logout_page', 10, 2);
function change_to_custom_logout_page($logout_url, $redirect) {
// Change to custom logout page
$logout_url = home_url('/custom-logout/');
return $logout_url;
}
Add a Custom Redirect for Specific User Roles
Redirect users with specific roles to a custom page after logging out.
add_filter('logout_url', 'redirect_by_user_role', 10, 2);
function redirect_by_user_role($logout_url, $redirect) {
// Get current user
$user = wp_get_current_user();
// Check for specific user role
if (in_array('editor', $user->roles)) {
$redirect = home_url('/editors-logout/');
} elseif (in_array('subscriber', $user->roles)) {
$redirect = home_url('/subscribers-logout/');
}
$logout_url = add_query_arg('redirect_to', urlencode($redirect), $logout_url);
return $logout_url;
}
Remove the Redirect Parameter from the Logout URL
Remove the redirect parameter from the logout URL, so the user stays on the same page after logging out.
add_filter('logout_url', 'remove_redirect_parameter', 10, 2);
function remove_redirect_parameter($logout_url, $redirect) {
// Remove the redirect parameter
$logout_url = remove_query_arg('redirect_to', $logout_url);
return $logout_url;
}