skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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.


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

    header("HTTP/1.1 200 OK");

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search