How to rewrite AJAX call URL to admin-ajax.php to match REST style URLs

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

Read More

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?

Related posts