wp_doing_ajax()

Determines whether the current request is a WordPress Ajax request.


Return Return

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


Top ↑

Source Source

File: wp-includes/load.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 );
}


Top ↑

Changelog Changelog

Changelog
Version Description
4.7.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content

    In a PHP code, if we want to differentiate its output behavior based on how it is being called (like a normal function or like a WP AJAX callback), then we should use the wp_doing_ajax() function like below,

    function wpdocs_render() {
    
    	/* ........ SOME LOGIC ....... */
    	
    	$return_data = array( 'success'  => true );
    	
    	if ( wp_doing_ajax() ) {
    		wp_send_json_success( $return_data );
    	} else {
    		return $return_data;
    	}
    
    }
    

You must log in before being able to contribute a note or feedback.