Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

3 Answers

up vote 11 down vote accepted

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

share|improve this answer
add comment

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;
share|improve this answer
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 May 2 '12 at 11:45
add comment

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

share|improve this answer
    
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 Sep 15 '10 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 Sep 16 '10 at 5:56
    
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 Sep 17 '10 at 17:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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