0

Suppose I want to create a new rewrite rule like the following:

add_action('init', 'fffl_router_add_rewrite_rules');
function fffl_router_add_rewrite_rules()
{
    add_rewrite_rule('books/([^/]+)/?$', 'index.php?book=$matches[1]', 'top');
}
add_filter('query_vars', 'fffl_router_add_query_vars');
function fffl_router_add_query_vars($vars)
{
    $vars[] = 'book';
}
function fffl_router_load_templates()
{
    if (fffl_book_is_valid(get_query_var('book'))) {
        //Load template with template_include filter
    }
}

To prevent malicious actions from evil users I have to validate the book param in the fffl_router_load_templates() function. If the param is valid I have to include the template (this works perfectly in my code) else I would like to obtain a 404 like what happens when the rewrite rule is not matched.

Is this possible without forcing manually the 404 error (ie. $wp_query->set_404() in the else clause of fffl_router_load_templates())?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.