The old_slug_redirect_url WordPress PHP filter allows you to modify the redirect URL when the old slug is changed.
Usage
add_filter('old_slug_redirect_url', 'your_custom_function', 10, 1); function your_custom_function($link) { // your custom code here return $link; }
Parameters
- $link (string) – The original redirect URL.
More information
See WordPress Developer Resources: old_slug_redirect_url
Examples
Append a query string to the redirect URL
Modify the redirect URL by appending a query string to track redirects from old slugs.
add_filter('old_slug_redirect_url', 'append_query_string', 10, 1); function append_query_string($link) { $link .= '?source=old_slug'; return $link; }
Redirect to a custom URL
Redirect users to a custom URL instead of the default redirect URL.
add_filter('old_slug_redirect_url', 'custom_redirect_url', 10, 1); function custom_redirect_url($link) { $custom_url = 'https://example.com/redirect-page'; return $custom_url; }
Redirect to the home page
Redirect users to the home page when the old slug is accessed.
add_filter('old_slug_redirect_url', 'redirect_to_home_page', 10, 1); function redirect_to_home_page($link) { return home_url(); }
Add a custom slug prefix
Add a custom prefix to the redirect URL to differentiate between old and new slugs.
add_filter('old_slug_redirect_url', 'add_custom_slug_prefix', 10, 1); function add_custom_slug_prefix($link) { $prefix = 'old-'; $link = str_replace(home_url(), home_url() . '/' . $prefix, $link); return $link; }
Prevent redirection for a specific old slug
Prevent redirection if the old slug matches a specific value and redirect to the home page instead.
add_filter('old_slug_redirect_url', 'prevent_specific_old_slug_redirection', 10, 1); function prevent_specific_old_slug_redirection($link) { $blocked_slug = 'blocked-slug'; if (strpos($link, $blocked_slug) !== false) { return home_url(); } return $link; }