php Ajax 是 WordPress 中的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14348470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is Ajax in WordPress
提问by Racura
Is there anyway to detect if the current server operation is currently an AJAX request in WordPress?
无论如何要检测当前服务器操作当前是否是 WordPress 中的 AJAX 请求?
For example:
例如:
is_ajax()
回答by webaware
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".
更新:从 WordPress 4.7.0 开始,您可以调用函数wp_doing_ajax()。这是更可取的,因为以不同方式“执行 Ajax”的插件可以过滤以将“假”变成“真”。
Original answer:
原答案:
If you're using Ajax as recommended in the codex, then you can test for the DOING_AJAXconstant:
如果您按照 codex 中的建议使用 Ajax ,则可以测试DOING_AJAX常量:
if (defined('DOING_AJAX') && DOING_AJAX) { /* it's an Ajax call */ }
回答by Nate Weller
WordPress 4.7has introduced an easy way to check for AJAX requests, so I thought I would add to this older question.
WordPress 4.7引入了一种简单的方法来检查 AJAX 请求,所以我想我会添加到这个较旧的问题中。
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.
说明:判断当前请求是否为 WordPress Ajax 请求。
返回:(bool) 如果是 WordPress Ajax 请求则为真,否则为假。
It is essentially a wrapper for DOING_AJAX.
它本质上是 DOING_AJAX 的包装器。
回答by Spencer Cameron-Morin
To see if the current request is an AJAXrequest sent from a js library ( like jQuery), you could try something like this:
要查看当前请求是否是从 js 库(如jQuery)发送的AJAX请求,您可以尝试以下操作:
if( ! empty( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) &&
strtolower( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ]) == 'xmlhttprequest' ) {
//This is an ajax request.
}
回答by Ignacio Jose Canó Cabral
I know this is an old thread, but there is an issue with the accepted answer,
我知道这是一个旧线程,但是接受的答案存在问题,
Checking for the defined DOING_AJAX constant will always be true, if the request is to the admin-ajax.php file. (https://core.trac.wordpress.org/browser/tags/4.4.2/src/wp-admin/admin-ajax.php#L16)
如果请求是针对 admin-ajax.php 文件,则检查定义的 DOING_AJAX 常量将始终为真。( https://core.trac.wordpress.org/browser/tags/4.4.2/src/wp-admin/admin-ajax.php#L16)
Sometimes admin-ajax.php hooks aren't used for AJAX request, just a simple endpoint (Paypal IPN for example).
有时 admin-ajax.php 钩子不用于 AJAX 请求,只是一个简单的端点(例如 Paypal IPN)。
The correct way is what Ian and Spencer have mentioned.
正确的方法是 Ian 和 Spencer 提到的。
if( ! empty( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) &&
strtolower( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ]) == 'xmlhttprequest' ) {
//This is an ajax request.
}
(would have commented... but no rep)
(会评论......但没有代表)
回答by Ian Brindley
I am not sure if WordPress has a function for this but it can be done by creating a simple one yourself.
我不确定 WordPress 是否具有此功能,但可以通过自己创建一个简单的功能来完成。
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{
// Is AJAX request
return true;
}
回答by adamj
if ( ! function_exists('is_ajax') ) {
function is_ajax() {
return defined( 'DOING_AJAX' );
}
}
回答by Kuliraj
I personally prefer the wp_doing_ajax(), but here is another example that should do it.
我个人更喜欢wp_doing_ajax(),但这是另一个应该这样做的例子。
if( true === strpos( $_SERVER[ 'REQUEST_URI' ], 'wp-admin/admin-ajax.php' ) ) {
// is doing ajax
}

