I'm working on a plugin for a custom post type whose real content is cached in a redis database.

When a user requests http://my-site/some-post my plugin retrieves the content from redis and returs it.

Now I need to serve the post child assets, ie a request to http://my-site/some-post/picture.jpg or http://my-site/some-post/subdir/media.mp3 should also be retrieved by the plugin from redis however I'm having a hard time to figure out how to handle those content in wordpress since they do not really exists in a directory. Wordpress always returns a 404.

Which filters or actions can I use to achieve what I want?


I think the earliest actions you can hook are muplugins_loaded and plugins_loaded. muplugins_loaded will only fire for Must Use Plugins.

Some plugins_loaded pseudo-code:

add_action( 'plugins_loaded', 'wpd_plugin_routes' );
function wpd_plugin_routes() {
    if( is_a_virtual_file() ){

If you want the full WordPress environment, plugins, theme, and authenticated user, right before WordPress parses the request, you've got the do_parse_request filter:

add_filter( 'do_parse_request', 'wpd_do_parse_request', 1, 3 );
function wpd_do_parse_request( $continue, WP $wp, $extra_query_vars ){
    // do something and halt execution, or
    return $continue; // true by default, meaning continue parsing the request

If you want all of the above after WordPress parses the request, you've got the parse_request action. Here you can alter the main query vars:

add_action( 'parse_request', 'wpd_parse_request_thing' );
function wpd_parse_request_thing( $query ) {
    if( isset( $query->query_vars['pagename'] ) ){
        if( 'some-page' == $query->query_vars['pagename'] ){
            $query->query_vars['pagename'] = 'some-other-page';

The last action right before the template is loaded is template_redirect. Here all of the is_ conditionals work:

add_action( 'template_redirect', 'wpd_page_template_redirect' );
function wpd_page_template_redirect() {
    if( is_404() ) {
        // do something
  • Thank you for such concise and yet complete answer !! The do_parse_request did the trick!
    – epc
    Apr 25 '18 at 19:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.