Is Ajax in WordPress Is Ajax in WordPress wordpress wordpress

Is Ajax in WordPress


Update: since WordPress 4.7.0 you can call a function wp_doing_ajax(). This is preferable because plugins that "do Ajax" differently can filter to turn a "false" into a "true".


Original answer:

If you're using Ajax as recommended in the codex, then you can test for the DOING_AJAX constant:

if (defined('DOING_AJAX') && DOING_AJAX) { /* it's an Ajax call */ }


WordPress 4.7 has introduced an easy way to check for AJAX requests, so I thought I would add to this older question.

wp_doing_ajax()

From the Developer Reference:

  • Description: Determines whether the current request is a WordPress Ajax request.

  • Return: (bool) True if it's a WordPress Ajax request, false otherwise.

It is essentially a wrapper for DOING_AJAX.


To see if the current request is an AJAX request sent from a js library ( like jQuery ), you could try something like this:

if( ! empty( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) &&      strtolower( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ]) == 'xmlhttprequest' ) {    //This is an ajax request.}