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.

i want to do something if my url is /pagethatdontexist But I dont want wp sending 404 headers. I tried this:

add_action('wp', array($this,'awp'));   
function awp(){
global $wp_query ;
debug($wp_query->is_404);
if($wp_query->query_vars['name'] == 'pagethatdontexist')
    debug('yes were at pagethatdontexist');;
$wp_query->is_404 = false;
debug($wp_query->is_404);
}

Which works, but still, 404 headers are sent.

Where can I hook in and tell wp not to send the 404?

EDIT -- well, this seems to do the trick, I'm not sure if it's Kosher.

function filter_status_header(  $status_header, $header, $text, $protocol ){
    global $wp_query;
    if (iDontWant404()){
        $wp_query->is_404 = false;// still sends 404 unless:
        return 'HTTP/1.1 200 OK';  
    }
    else
        return $status_header;
}
add_filter('status_header',array($this,'filter_status_header'),4,4);
share|improve this question
    
What do you want to do instead of going to the 404 page? –  Charles Clarkson Sep 4 '13 at 20:16
    
Wul, whatever I want. I'll figure sth out from there. First I've got to get it to not 404 if we are on a url that I want. –  Doug Cassidy Sep 4 '13 at 20:43

2 Answers 2

up vote 0 down vote accepted

You can use an earlier action, request, to do whatever you need. It allows you to modify query variables, or even cancel the entire request without issuing a 404 error:

function cancel_request_parsing_112929( $query_vars ) {
    if ( in_array( 'pagethatdontexist', $query_vars ) ) {
        return array();
    }

    return $query_vars;
}
add_action( 'request', 'cancel_request_parsing_112929' );

I used in_array() here instead of a direct comparison, because the actual match depends on your permalink structure. It would be name for /%postname%/, category_name for /%category%/%postname%/, pagename for /%year%/%monthnum%/%postname%/, etc.

You can also check $_SERVER['REQUEST_URI'] instead.

share|improve this answer

As someone (somewhere) said it's not a good idea to fall into a 404 without sending headers. The right way to do things is to redirect somewhere else.

If you fall into the 404 template, you'll have the 404 headers sent as logic as is. Search engines will also love you!

Cheers.

share|improve this answer

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.