33

Provided you have a 404 page defined in your theme, Wordpress will display a 404 page if "tag" is defined in $wp_query->query_vars, and there are no posts matching that tag.

I'm writing a plugin that displays some information on each page, in addition to posts. I'd like to alter the 404 logic so that the 404 page gets displayed if there are no posts matching a tag and the plugin cannot pull up any data matching that tag. If the plugin can find data, I'd like to show a normal page, regardless of whether there are posts on that page or not ...

I've been Googling, reading code, reading the codex, and poking around here, and haven't been able to figure out where Wordpress triggers that 404, and how I can override it. (I have a feeling it might have something to do with status_header() in functions.php, but it's not clear how and when I need to hook into it).

Any help/ideas/enlightenment appreciated.

Thank you,

~ Patch

4 Answers 4

34

After a bit more slogging through code and Googling, I found the answer. It's contained in this thread (see Otto42's post), but for the record, adding the following to your plugin will override the 404 handling for the conditions you specify:

add_filter('template_redirect', 'my_404_override' );
function my_404_override() {
    global $wp_query;

    if (<some condition is met>) {
        status_header( 200 );
        $wp_query->is_404=false;
    }
}

Note that you need to set "is_404" to false before PHP outputs headers, which is why hooking it in the template_redirect logic is a good idea.

~ Patch

1
  • took me days to find this. I have been poking around, tried different things that didn't work
    – Emeka Mbah
    Commented Jul 11, 2021 at 17:28
5

I needed to do the same for a custom project where there was always a 200 page, and found you can also simply add this to the top of your template file ( above get_header(); )

global $wp_query;
status_header( 200 );
$wp_query->is_404=false;
1
  • 1
    Great! This worked for me. I was making my script and it generated 404. I tried runnig scripts from wp->main but that was not a ver clean solution. This worked for me: // configuration require('./wp-load.php'); // init WP (inits objects, language and stuff). wp(); // fix title (this is not 404) $wp_query->is_404 = false; // doesn't fix the actual 404 status status_header( 200 ); // but this does :-)
    – Nux
    Commented May 2, 2012 at 11:45
1

Create 404.php template file in your theme and customize as needed (adding your plugin output or whatever). You mention defined in your theme, what exactly is wrong with this approach for you?

Codex Creating an Error 404 Page

3
  • 1
    What I'm looking for is a way to alter the circumstances under which that 404 page gets triggered. Right now, if Wordpress can't find any posts matching a tag in the query vars, the 404 page appears. I'd like to drop in some additional code that ensures the 404 page only appears if Wordpress can't find any posts and my plugin can't find any supplemental information to display.
    – Patch
    Commented Sep 15, 2010 at 20:04
  • Purpose of 404 is for visitor to know link is wrong and for you to log it and fix (if needed). You want to neither tell visitor that he is using broken link or be aware about that yourself. Displaying information on 404 works just fine, hijacking it is not a requirement, so why bother? Just trying to grasp your logic and/or technicalities.
    – Rarst
    Commented Sep 16, 2010 at 5:56
  • 1
    What I was looking for was a way to change the definition of what pages are "wrong". The site I'm building has a blog component, but that's not all the site is. I'm using Wordpress tags both to display Wordpress posts, and pull up additional content via the plugin. I want to return a 200 status code if either a) there are posts or b) my plugin can find stuff to display that matches the tag. Thus the need to rewire the logic. Regardless, thank you for taking the time to write an answer -- sorry that my problem was weird enough to need a more involved answer :-)
    – Patch
    Commented Sep 17, 2010 at 17:25
1

Since most answers here are very old and I still stumbled upon this post, I'm adding this for good measure. It looks like WordPress introduced a new hook in version 4.5.0:

https://developer.wordpress.org/reference/hooks/pre_handle_404/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.