• 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: March 13, 2023

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] Let’s Make Plugin E01: A Simple View Count Plugin

[WordPress] Create custom post type programmatically

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

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

Hi! I am a Vietnamese coder living in Oulu, Finland. Currently I am working with PHP, MySQL, HTML, CSS, and JavaScript. This blog is my learning diary, in which I share knowledge from my own experience or research. Hopefully you can find something useful here and don’t hesitate to open a discussion or leave a feedback!

FOLLOW MY BLOG

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