How can I get something like this to work?
// in a plugin / theme:
// This imaginary function will make WordPress think that the
// current request is a 404.
// Ideally this function could be ran anywhere, but realistically it
// will probably have to be before the template_redirect hook runs.
generate_404_somehow();
// later...
add_action('template_redirect', function() {
// should be "true"
var_dump(is_404());
});
Basically under certain conditions, I want to tell WordPress to show its 404 template (which I can hook into later if I want) instead of the template it’s about to load (eg a page or archive).
I know I could just do a 302
redirect to a non-existent page but that’s very messy. I could also send a 404
HTTP header manually, but then I can’t use WP’s nice 404 page (I already have things that hook into is_404()
that need to get fired at the right time).