1

I've read this post

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

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?

  • why not just direct requests to index.php? you can execute ajax hooked functions via do_action. look in the admin-ajax.php file, it would be fairly simple mimic. – Milo Mar 15 '14 at 1:43
  • Sorry, didn't get your point. I can't even get any success on the redirect part, seems like the add_rewrite_rule function doesn't work on multisite installation. – arnaldo2204 Mar 15 '14 at 5:29
  • I guess I don't understand how anything you're trying to do is related to the admin side of things. if you're just trying to provide an API endpoint for your plugin, the answer linked in the second comment of your other question seems like what you need (and is exactly how I handle this myself), but it's not clear why that doesn't work for you. – Milo Mar 15 '14 at 7:06
  • Yes, that answer is exactly what I tried to put together, but that doesn't work in the sense that I get 404s all the time. The query_vars and parse_request filters never get called, I have no idea why. Do you? Is it related to multisite installation, perhaps? I've seen complaints of rewrite rules added through add_rewrite_rule function not working on multisite platform. – arnaldo2204 Mar 15 '14 at 17:34
3

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' );
  • Milo, thanks for the reply. This is exactly the code I have in place, but it doesn't work. I added debug lines in the query_vars and parse_request filters, those functions never get called. I removed the .php from the rule as you mentioned, flushed the rules from permalink settings page and also from code, nothing works. Still goddamn 404s. Any insights? – arnaldo2204 Mar 15 '14 at 22:02
  • I tested it in a multisite subdirectory install, both example.com/my-api/ and example.com/subsite/my-api/ and both work as expected. do you have a subdirectory setup, or are you using subdomains? have tried disabling all plugins and using a core-bundled theme? – Milo Mar 16 '14 at 0:53
  • 1
    Milo, thanks again. I was using subdomain setup, but your question gave me some insight and I tried some different setups, with subdirectory and subdomain. I took a different machine and installed WP in apache, and both envs worked OK. Then I narrowed my suspicion on Windows IIS (my original main env) and then found out the rewrite module for IIS was actually missing. I installed it and then imported htaccess rules to it. Bingo, all worked fine. Thanks for the help and patience! – arnaldo2204 Mar 17 '14 at 6:50

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.