Custom query_vars and parse_request on wp-admin

I’ve read this post

http://wordpress.org/support/topic/query-vars-in-admin

Read More

and although it’s quite an old question, it suits me perfectly. I’m trying to accomplish what I already posted here:

Rewrite rule for admin-ajax.php

But in the code below, only the first filter gets called while in wp-admin:

add_action('init', array('MYCLASS', 'add_custom_rewrite_rules'), 10, 1);
add_action('query_vars', array('MYCLASS', 'add_custom_query_vars'), 10, 1);
add_action('parse_request', array('MYCLASS', 'add_custom_parse_request'), 10, 1);

I can’t get this to work. It seems the two last filters are not called while navigating inside wp-admin. Is that true? How can I get around this?

Related posts

Leave a Reply

1 comment

  1. I think this does what you’re trying to achieve. Regarding multisite 404s, you have to flush rewrite rules on every site within multisite where you want this rule to get added. A quick method for testing purposes is to visit the permalinks settings page for each site you’re testing. Also note, I removed the .php extension from your rule. I think you’ll otherwise get strange behavior – REQUEST will curiously be an empty array.

    function wpd_api_rule(){
        add_rewrite_rule( 'my-api/?$', 'index.php?my-api=1', 'top' );
    }
    add_action( 'init', 'wpd_api_rule' );
    
    function wpd_query_vars( $query_vars ){
        $query_vars[] = 'my-api';
        return $query_vars;
    }
    add_filter( 'query_vars', 'wpd_query_vars' );
    
    function wpd_parse_request( $wp ){
        if( array_key_exists( 'my-api', $wp->query_vars ) ) {
            // AJAX API example -
            // you might want to whitelist actions here
            // use wp_ajax_nopriv_ if not logged in
            do_action( 'wp_ajax_' . $_REQUEST['action'] );
            die(0);
        }
    }
    add_action( 'parse_request', 'wpd_parse_request' );