这是因为您没有排除ajax请求。
检查以下示例the WordPress Codex for the admin_init
hook 也适用于init
钩住并查看AJAX请求如何排除在if
使用DOING_AJAX
常数:
/**
* Restrict access to the administration screens.
*
* Only administrators will be allowed to access the admin screens,
* all other users will be shown a message instead.
*
* We do allow access for Ajax requests though, since these may be
* initiated from the front end of the site by non-admin users.
*/
function restrict_admin() {
if ( ! current_user_can( \'manage_options\' ) && ( ! defined( \'DOING_AJAX\' ) || ! DOING_AJAX ) ) {
wp_die( __( \'You are not allowed to access this part of the site\' ) );
}
}
add_action( \'admin_init\', \'restrict_admin\', 1 );
您可以将此作为起点:
add_action( \'init\', \'wpse_182220_init\' );
function wpse_182220_init() {
// Exit function if doing an AJAX request
if ( defined( \'DOING_AJAX\' ) && DOING_AJAX ) {
return;
}
echo \'OK, not doing AJAX\';
}
道具到
RRikesh 感谢他在评论中提到这一点。