WordPress at a glance

wp_doing_ajax() WP 4.7.0

Determines whether the current request is a WordPress Ajax request.

This function is a wrapper for:

defined( 'DOING_AJAX' ) && DOING_AJAX
✈ 1 time = 0.000007s = speed of light | 50000 times = 0.01s = speed of light | PHP 7.0.8, WP 4.7
Hooks in function
Return

true/false. True if it's a WordPress Ajax request, false otherwise.

Usage

if( wp_doing_ajax() ){
	// this is AJAX request
}

### Examples

#### #1 Do something if the current request is an AJAX request

Add AJAX hooks only for AJAX request.

```php
if( wp_doing_ajax() ){

	add_action( 'wp_ajax_myaction',        'ajax_handler' );
	add_action( 'wp_ajax_nopriv_myaction', 'ajax_handler' );

	function ajax_handler(){
		// ajax request handler
	}
}

Code of wp doing ajax: wp-includes/load.php WP 5.2.1

<?php
function wp_doing_ajax() {
	/**
	 * Filters whether the current request is a WordPress Ajax request.
	 *
	 * @since 4.7.0
	 *
	 * @param bool $wp_doing_ajax Whether the current request is a WordPress Ajax request.
	 */
	return apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX );
}

Related Functions

From tag: AJAX

More from tag: Conditional tags (page type and request)

More from tag: Conditional tags (all)

More from category: Helper Functions

No comments
    Hello, !     Log In . Register