5

Is there any reason to use admin-ajax.php for ajax requests versus a custom page template?

I didn't know about admin-ajax.php until recently, so what I had been doing is creating a custom page template like this:

<?php
/**
 * Template Name: API
 */

if ( isset( $_GET['ajax_request'] ) ) {
// do stuff
}

And the ajax call would be to the URL http://mysite.com/api/, which is where I've published a blank page using my API page template. This seems to give me access to all my WordPress functions and spit out data.

However, recently I've read up on admin-ajax.php and understand another way to connect to the WordPress database is to call the URL http://mysite.com/wp-admin/admin-ajax.php and have functions like this:

add_action( 'wp_ajax_nopriv_action', 'my_do_stuff' );
add_action( 'wp_ajax_show_action', 'my_do_stuff' );

function my_do_stuff() {
// do stuff
}

Is it wrong to connect the first way? Does admin-ajax.php provide extra security or something? Thanks any input!

1
  • BTW: if you want to make a api on your wordpress site and want a url that make sense as api request url. Then you can do some mod rewrite so http://site.com/api/ maps to http://site.com/wp-admin/admin-ajax.php
    – Sisir
    Commented Jul 22, 2012 at 20:38

2 Answers 2

7

First, the obvious drawback to the first method is that it depends on your specific page, template, and permalink structure to all work correctly. Using admin-ajax.php will work correctly in any context, theme or plugin, where proper WordPress best practices are followed.

The less obvious drawback to the first method is that it uses more memory than doing WordPress-enabled AJAX calls, since the whole WordPress environment is loaded, as it's presumed that a front-end or admin page will be output.

The addition of NONCEs with admin-ajax.php provides easy, built-in security.

1
  • 1
    To be fair, the entire WordPress environment + the admin area is loaded for admin-ajax.php as well. The only thing that gets skipped is the template loader. admin-ajax.php is still the correct way, of course. :) Commented Jul 22, 2012 at 19:03
3

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 some cache systems 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 allows 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.

4
  • Can you expand upon this? I see no reason you can't cache an AJAX call using admin-ajax.php.
    – s_ha_dum
    Commented Dec 4, 2013 at 16:30
  • Sure. It may be perfectly possible to cache the DB query, objects, etc involved in an admin-ajax.php call, I'm actually not too sure about the details of that. But if you're using something that does URL-based caching, admin-ajax.php isn't going to be involved — since all your AJAX requests are to that one URL with just different parameters passed in. However, if you go the template_redirect() route, your AJAX calls for different posts will actually be requests to different URLs, allowing a URL-based caching system to work its magic.
    – Drywall
    Commented Dec 4, 2013 at 16:48
  • So what you are talking about is strictly URL-based caching, but you mentioned hooks. There are a number of hooks that should allow caching of requests to admin-adjax.php, plus, if the OP can write object caching into the callbacks. I would suggest you edit the answer to clarify/caveat it a bit.
    – s_ha_dum
    Commented Dec 4, 2013 at 17:20
  • Thanks. I have amended to eliminate the reference to hooks and to clarify. URL-based caching is the most clear-cut situation, but as my amended statement mentions, if whatever caching system is in place backs off within the admin area (which is a possibility though I can't definititely say any plugins do/don't do that) then you're still missing out. Better?
    – Drywall
    Commented Dec 4, 2013 at 18:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.