• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Ani's Webdev Blog

A learning diary of website development

  • Home
  • Demo
    • Ajax Contact Form
  • WordPress
  • Front-end
  • Backend

WordPress: Redirect archive 404 pages to blog post page

Modified on: July 4, 2020

If you restructure tags and categories of your WordPress website, for example renaming, removing, or deleting, you would have a lot of broken links made by the change. This post shows you how to make a redirect that all 404 archive pages will be forwarded to the blog post page, or a new page that you define.

Here is the code that you can place into the file functions.php of the theme:

Plain Text

It is quite clear what it will do. First, before WordPress checks which template to load (with template_redirect hook), the function archive_to_blog will request the url from the browser address bar.

If it is an archive page, the expected result would be: tag/tagname or category/categoryname.

Then, we extract the returned url with explode. After that, we check if the first part of the url is are tag or category. If not, returns at once. If they are, then continue checking are they broken links with WordPress built-in function is_404().

Finally, just 301 redirect them into whatever link you want.

The downside of this solution is that it checks every links return, to find if the page is a 404 tag or category page. It is somehow unnecessary in my opinion.

References

  • template_redirect hook
  • Print out page template with get_page_template()

Filed Under: Backend, Wordpress, WordPress Themes Tagged With: php

Recent posts

WordPress: Create custom post type programmatically

[WordPress] Open Images in Posts into Lightboxes On Clicks (Without Plugins)

Mangools Review: A Productive SEO tool for The Affiliates

Reader Interactions

You are here: Home / Backend / WordPress: Redirect archive 404 pages to blog post page

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Hello! My name is Anh. I am a front-end developer in Oulu, Finland. Currently, I work with PHP, HTML, CSS, JavaScript and love writing about them. Please feel free to join the discussion!

FOLLOW MY BLOG

Thank you for visiting this website! I hope you find something useful here :). Contact me by email: anh@anhkarppinen.com.