What’s the best way to implement AJAX in WordPress?

WordPress has a framework in place for handling AJAX which is well documented here: http://codex.wordpress.org/AJAX_in_Plugins

My concern with this is that every time you execute an AJAX query, (even if you want to return a single field from a table) this framework instantiates the whole WordPress stack, including any plugins and theme functions that may exist on your site. This strikes me as a pretty heavy method to perform what may end up being a pretty simple function.

Read More

Typically a lot of WordPress sites run on shared hosting and don’t have a whole lot of processing power, so, based on my experiences, a reponse time of 2 seconds to a WordPress hosted AJAX call is not uncommon.

Any recommendations on how to improve on this response time?

Related posts

Leave a Reply

1 comment

  1. The crude alternative most people resort to is the one where you add a file and include several levels up. This is arguably worse for performance if not just as bad.

    But there is a variant that works better, if we create our own AJAX entrypoint URL, and specify:

    define( 'SHORTINIT',true);
    

    Then include the WordPress environment, we can skip over the majority of the loading process. This is the way to go if you want super zippy AJAX requests. The downside is that we have to load some things ourselves, and some steps of the loading process are never executed.

    See this answer for more detail Ajax takes 10x as long as it should/could