mondi
mondi

Reputation: 559

How to disable 404 header on specific Wordpress url

I use this code to redirect specific url to a php file on my custom made Wordpress theme:

add_action('wp', function() {
if ( trim(parse_url(add_query_arg(array()), PHP_URL_PATH), '/') === 'pagename' ) {
include(locate_template('mysciprt.php'));
exit();
}});

(I know i can use rewrite rules but i need to use this code for a few reasons).

It's work's great. the problem is the page still has a http header with 404 status. and that's a problem for a few reasons (some of them are: seo, post scripts cant be used...).

My question is: Is there any way to fix it?

What i have tried:

I tried adding:

global $wp_query;
status_header(200);
$wp_query->is_page = true;
$wp_query->is_404  = false;

Didn't help at the beginning, then i changed my action('wp'.. to action('init'... and it works! but the problem is the reason i am using the 'wp' and not 'init' is because i need to use get_query_var() on this script and i can't when using the 'init' action.

Upvotes: 0

Views: 2841

Answers (2)

mondi
mondi

Reputation: 559

End up to be simpler then i thought:

Just added:

global $wp_query;
status_header( 200 );
$wp_query->is_page = true;
$wp_query->is_404=false;

To the script.php file itself before anything else.

Upvotes: 8

delboy1978uk
delboy1978uk

Reputation: 12365

Just send a header with a 200 status code. Try this:

header("HTTP/1.1 200 OK");

Upvotes: -1

Related Questions