正在查看the WordPress source code, 我可以这么说wp_ajax_nopriv_*
仅在您未登录时激发,并且wp_ajax_*
否则会引发火灾。
以下是相关位,在admin-ajax.php
, 版本5.0.3中的第85-115行:
if ( is_user_logged_in() ) {
// If no action is registered, return a Bad Request response.
if ( ! has_action( \'wp_ajax_\' . $_REQUEST[\'action\'] ) ) {
wp_die( \'0\', 400 );
}
/**
* Fires authenticated Ajax actions for logged-in users.
*
* The dynamic portion of the hook name, `$_REQUEST[\'action\']`,
* refers to the name of the Ajax action callback being fired.
*
* @since 2.1.0
*/
do_action( \'wp_ajax_\' . $_REQUEST[\'action\'] );
} else {
// If no action is registered, return a Bad Request response.
if ( ! has_action( \'wp_ajax_nopriv_\' . $_REQUEST[\'action\'] ) ) {
wp_die( \'0\', 400 );
}
/**
* Fires non-authenticated Ajax actions for logged-out users.
*
* The dynamic portion of the hook name, `$_REQUEST[\'action\']`,
* refers to the name of the Ajax action callback being fired.
*
* @since 2.8.0
*/
do_action( \'wp_ajax_nopriv_\' . $_REQUEST[\'action\'] );
}
因此,如果您登录(即,
is_user_logged_in()
是
true
), 它运行
wp_ajax_*
操作,否则将运行
wp_ajax_nopriv_*
行动。
如果无论用户是否登录,都希望运行相同的操作,我建议您both wp_ajax_*
和wp_ajax_nopriv_*
.