What is WordPress Hook: old_slug_redirect_url
The old_slug_redirect_url hook in WordPress is used to redirect old URLs to new ones. This can be useful when restructuring a website and needing to ensure that any old links still lead to the correct content.
Understanding the Hook: old_slug_redirect_url
The old_slug_redirect_url hook is located within the WordPress redirection process. It allows developers to specify the old URL and the new URL to which it should be redirected.
Hook Parameters (if applicable): old_slug_redirect_url
The old_slug_redirect_url hook accepts two parameters: the old URL and the new URL. These parameters are used to define the redirection rule for the old URL.
Hook Doesn’t Work: old_slug_redirect_url
If the old_slug_redirect_url hook doesn’t work, it could be due to incorrect syntax in the code or conflicts with other plugins or themes. It’s important to double-check the code for any errors and deactivate other plugins to see if there are any conflicts.
Best Practices & Usage Notes (if applicable): old_slug_redirect_url
When using the old_slug_redirect_url hook, it’s important to test the redirection thoroughly to ensure that it is working as expected. Additionally, it’s recommended to use this hook sparingly and only for necessary redirections to avoid impacting site performance.
old_slug_redirect_url Usage Example: old_slug_redirect_url
“`php
function custom_old_slug_redirect() {
if ( is_404() ) {
$old_url = ‘/old-post-url/’;
$new_url = ‘/new-post-url/’;
if ( $old_url === $_SERVER[‘REQUEST_URI’] ) {
wp_redirect( $new_url, 301 );
exit;
}
}
}
add_action( ‘template_redirect’, ‘custom_old_slug_redirect’ );
“`