Setting data for non existing pages (overriding 404's) in Wordpress

129 Views Asked by At

I have the following problem:

I have a couple of non-existing pages, for instance when there's 'detail' in the URL followed by an ID, as in mydomain.com/detail/12345/

In this case I am overriding the 404 with do_action, and including a template, as follows:

if($detail):
    do_action('override404', 'detail');
    add_filter('template_include', function () {
        return get_template_directory() . '/templates/page/detail-page.php';
    });
endif;

add_action(
    'override404',
    function ($name) {
        global $wp_query;
        status_header(200);
        $wp_query->is_single = true;
        $wp_query->is_404 = false;
    },
    10,
    1
);

So far, so good. As you can see the priority of this action is 10.

Now, in another action I want to check the value of is_single(), but it always is set to false.

Something like this:

function enqueue_api_scripts() {
    var_dump(is_single());
}
add_action('wp_enqueue_scripts', 'enqueue_api_scripts', 11);

As you can see the priority of this action is 11.

I am not seeing what I am doing wrong here. The reason I want to be able to check if is_single() equals true is that I don't want to use a global variable to do so, and that I also want to add some hooks to change the title's value and such. Any help is much appreciated.

0

There are 0 best solutions below