admin-ajax.php
isn't always the right way to go. If you're looking to fetch a post, for example, you may actually better off using something like template_redirect()
to load a custom template that returns JSON (or whatever you need returned).
Why? Caching. When you use admin-ajax.php
you're basically eliminating the opportunity for anysome cache systemsystems to save the output of the server response (generally admin URLs, and specifically admin-ajax.php, should not be cached). Using template_redirect()
on the other hand maintains the various hooks and whatnot aallows for maintaining separate URLs that many cache plugins and HTTP accelerators would be likely to use to keep the data. Even some back-end caching systems may not involve themselves in admin-ajax.php
if they're configured to avoid caching when is_admin()
.
Of course, if you're not getting something fairly static like a post, caching could actually be a really bad thing... in which case admin-ajax.php
is a far better choice.