A snippetclub.com member was looking for a way to redirect a bunch of old blog posts to the blog archive page. The solution I came up with was to store an array of all the URLs to match against, if there was a match, we could then redirect the user to our blog page.
1. Hook into the template_redirect action.
We will first hook into the template_redirect action built into WordPress. As quoted from the WordPress docs:
This action hook executes just before WordPress determines which template page to load. It is a good hook to use if you need to do a redirect with full knowledge of the content that has been queried.
/* * tct_redirect_old_posts * * @function Redirects a list of posts to a specific page * @author Taylor Drayson * @since 25/05/2023 */ add_action('template_redirect', 'tct_redirect_old_posts'); function tct_redirect_old_posts() { // Should redirect? $redirect = tct_handle_post_redirects(); if ($redirect === null){ return; } wp_safe_redirect($redirect, 301); exit; }
Copy
Code breakdown:
- In our hook, we are going to run our redirect handler function. This will either return a URL, or null depending on the current URL the user has visited.
- If our handle redirect function returns null, we do nothing and let WordPress continue to load the page it was going to load.
- However, if we return a URL, then we are going to use the wp_safe_redirect function to return a 301 redirect to our returned URL.
2. Old posts list
We now need to create a function that stores an array of all the URLs we want to match.
function tct_old_posts_list(){ return [ '/old-page', '/super-old-page', ]; }
Copy
Code breakdown:
- In this function we are creating a separate item in the array for each URL path we need to match.
- You can have complex paths if your website has them.
- For example you can have
/my-old-page
and also/blog/my-old-article
which has/blog/
in front of the end part of the path. - NOTE: Ensure you have the / (slash) at the beginning as this is how we will be matching against in all our validations later.
3. Should we redirect? function
Now we want to create our function which will actually run the logic.
function tct_handle_post_redirects(){ $target_url = '/blog'; // url to redirect to if match // Get the current URL $current_url = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; // Extract the path from the URL $path = parse_url($current_url, PHP_URL_PATH); if ($path === null) { return $url; } $path = strtolower($path); $path = rtrim($path, "/"); $urls = tct_old_posts_list(); if (in_array($path, $urls)){ return site_url($target_url); } return null; }
Copy
Code breakdown:
- The first thing we have done is set the target URL. This is where we will redirect to if our function finds any matches.
- Next we are getting the current URL that the user has visited and extracting just the path. This will be everything after your domain.
- For example:
https://snippetclub.com/my-blog-post
would return the path/my-blog-post
- Another example:
https://snippetclub.com/article/another-article
would return the path/article/another-article
- For example:
- We have a couple of checks and formatting, to ensure consistent validation of our URLs.
- Finally we have our function that we created early that contains our array of old URL paths.
- We run a check to search if our
$path
matches any of the paths inside our array. - If a match is found, we should return our target URL we set earlier, otherwise we return null.