1

Let's say I have an AJAX callback setup using admin-ajax.php that fetches WordPress posts when query parameters are passed in the URL. A typical URL for an AJAX call using the GET method would look like:

http://example.com/wp-admin/admin-ajax.php/?action=get_posts&user_id=1

Now I would like to rewrite this to something like:

http://example.com/rest/posts/1

I'm able to rewrite URL for pages using WordPress native generate_rewrite_rules hook. But that doesn't work for admin-ajax.php as Wordress hasn't even loaded when admin-ajax.php is called. Any ideas on how to get this setup?

8
  • Have you seen and tried this or this?
    – fuxia
    Commented Jun 28, 2013 at 12:41
  • I did try earlier, the htaccess hack that you suggested in the answer that you linked. Though it serves the purpose of rewriting the AJAX url to a different location, I'm not sure how it could be modified to map "multiple" query parameters. Like in example above posts should refer to action=get_posts and 1 should refer to user_id=1. And if I have an additional query parameter, let's say page then that should also map correctly.
    – John
    Commented Jun 28, 2013 at 13:06
  • Inspect $_SERVER['REQUEST_URI'] like in the second link.
    – fuxia
    Commented Jun 28, 2013 at 13:08
  • I took a look at the second link but could not find anything relevant to $_SERVER['REQUEST_URI']. Am I missing something?
    – John
    Commented Jun 28, 2013 at 13:17
  • The method get_api_values() does it: strtok( $request, '/' ); separates the values from each other.
    – fuxia
    Commented Jun 28, 2013 at 13:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.