Es Ajax en WordPress


¿Hay de todos modos para detectar si la operación actual del servidor es actualmente una solicitud AJAX en WordPress?

Por ejemplo:

is_ajax()
Author: Spencer Cameron-Morin, 2013-01-16

6 answers

Si estás usando AJAX como se recomienda en el códice , entonces puedes probar la constante DOING_AJAX:

if (defined('DOING_AJAX') && DOING_AJAX) { /* it's an AJAX call */ }
 73
Author: webaware,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-01-16 01:07:36

WordPress 4.7 ha introducido una forma fácil de comprobar si hay solicitudes AJAX, así que pensé en agregar a esta pregunta anterior.

wp_doing_ajax()

De la Referencia del desarrollador :

  • Descripción: Determina si la solicitud actual es una solicitud Ajax de WordPress.

  • Return: (bool) True si se trata de una solicitud Ajax de WordPress, false de lo contrario.

Es esencialmente una envoltura para DOING_AJAX.

 16
Author: Nate Weller,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-27 17:13:20

Para ver si la solicitud actual es una solicitud AJAX enviada desde una biblioteca js (como jQuery ), puede intentar algo como esto:

if( ! empty( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) &&
      strtolower( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ]) == 'xmlhttprequest' ) {
    //This is an ajax request.
}
 13
Author: Spencer Cameron-Morin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-01-15 23:04:30

No estoy seguro de si WordPress tiene una función para esto, pero se puede hacer creando uno simple usted mismo.

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{
    // Is AJAX request
    return true; 
}
 5
Author: Ian Brindley,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-12-27 23:35:23
if ( ! function_exists('is_ajax') ) {
    function is_ajax() {
        return defined( 'DOING_AJAX' );
    }
}
 4
Author: adamj,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-25 08:14:23

Sé que este es un hilo viejo, pero hay un problema con la respuesta aceptada,

La comprobación de la constante definida DOING_AJAX siempre será verdadera, si la solicitud es para admin-ajax.archivo php. ( https://core.trac.wordpress.org/browser/tags/4.4.2/src/wp-admin/admin-ajax.php#L16 )

A veces admin-ajax.los hooks de php no se usan para solicitudes AJAX, solo para un simple endpoint (Paypal IPN por ejemplo).

La forma correcta es lo que Ian y Spencer tienen indicado.

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

(habría comentado... pero no rep)

 4
Author: Ignacio Jose Canó Cabral,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-14 13:53:14