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?
posts
should refer toaction=get_posts
and1
should refer touser_id=1
. And if I have an additional query parameter, let's saypage
then that should also map correctly.$_SERVER['REQUEST_URI']
like in the second link.$_SERVER['REQUEST_URI']
. Am I missing something?get_api_values()
does it:strtok( $request, '/' );
separates the values from each other.