我正在调用管理ajax。php在自定义设置中更改前端帖子(CPT广告)(草稿/发布)的状态。这对管理员用户很好。但是,在对订阅服务器和控制台执行相同操作时。记录我得到的响应,返回主页的全部内容(甚至没有错误)。
下面是我调用ajaxurl变量的代码:
// AJAXURL VAR
add_action(\'wp_head\', \'aii_ajaxurl\');
add_action(\'admin_head\', \'aii_ajaxurl\');
function aii_ajaxurl() {
echo \'<script type="text/javascript">
var ajaxurl = "\' . admin_url(\'admin-ajax.php\') . \'";
var nonce = "\' . wp_create_nonce( \'aii_nonce\' ) . \'";
</script>\';
}
下面是我处理ajax调用的代码:
// AJAX MYACCOUNT CHANGE STATUS
add_action(\'wp_ajax_aii_ajax_status_advert\',
\'aii_ajax_status_advert\');
add_action(\'wp_ajax_nopriv_aii_ajax_status_advert\',
\'aii_ajax_status_advert\');
function aii_ajax_status_advert() {
$result = \'\';
global $current_user;
wp_get_current_user();
$permission = check_ajax_referer( \'aii_nonce\', \'nonce\', false );
if( $permission != false ) {
if ( is_user_logged_in() ) {
$advert_id_sanitized = sanitize_text_field($_POST[\'id\']);
if(is_numeric($advert_id_sanitized)){
$advert_id_clean = $advert_id_sanitized;
$post_author_id = get_post_field( \'post_author\', $advert_id_clean );
if($current_user->ID == $post_author_id){
$advert_status = sanitize_text_field($_POST[\'status\']);
if($advert_status == \'draft\'){
$advert_status_clean = \'publish\';
}
if($advert_status == \'publish\') {
$advert_status_clean = \'draft\';
}
$result = wp_update_post( array(
\'ID\' => $advert_id_clean,
\'post_status\' => $advert_status_clean
));
$result = \'success\';//var_dump($_POST);
}
}
}
} else {
$result = \'error\';
}
echo $result;
die();
}
最后,这是我的jQuery AJAX调用:
jQuery(\'.aii-table-col.change-status a\').on(\'click\', function(e) {
e.preventDefault();
changeNumber = jQuery(this).attr(\'href\');
changeNumber = changeNumber.slice(1);
changeNonce = jQuery(this).data(\'nonce\');
changeStatus = jQuery(this).data(\'status\');
changeThis = jQuery(this);
jQuery.ajax({
type: \'post\',
url: ajaxurl,
data: {
action: \'aii_ajax_status_advert\',
nonce: changeNonce,
id: changeNumber,
status: changeStatus
},
success: function( result ) {
console.log(result);
if( result == \'success\' ) {
if(changeStatus==\'draft\'){
console.log(\'live now\');
changeThis.find(\'i\').removeClass(\'fa-play-circle-o\');
changeThis.find(\'i\').addClass(\'fa-pause-circle-o\');
changeThis.data(\'status\',\'publish\');
changeThis.parent().parent().find(\'.status\').empty().text(\'LIVE\');
jQuery(\'.aii-toast\').empty().append(\'<i class="fa fa-info-circle" aria-hidden="true"></i> Advert is now live\').fadeIn(250);
setTimeout(function() {
jQuery(".aii-toast").fadeOut(500)
}, 3000);
}
if(changeStatus==\'publish\'){
console.log(\'pause now\');
changeThis.find(\'i\').removeClass(\'fa-pause-circle-o\');
changeThis.find(\'i\').addClass(\'fa-play-circle-o\');
changeThis.data(\'status\',\'draft\');
changeThis.parent().parent().find(\'.status\').empty().text(\'PAUSED\');
jQuery(\'.aii-toast\').empty().append(\'<i class="fa fa-info-circle" aria-hidden="true"></i> Advert is now paused\').fadeIn(250);
setTimeout(function() {
jQuery(".aii-toast").fadeOut(500)
}, 3000);
}
}
}
})
});
我已经添加了wp\\u ajax\\u nopriv操作,即使您这个订阅者必须登录才能起草帖子(广告CPT),因此根据以下内容:
https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_nopriv_(action) 甚至不需要它。伙计们,我错过了什么?为什么管理员可以这样做,但订阅者却奇怪地返回(当console.log将数据记录到主页的全部内容时)。
最合适的回答,由SO网友:Sunil Dora 整理而成
我也面临同样的问题,
在检查我的functions.php 代码,我发现以下代码。
add_action(\'init\', \'blockusers_init\');
function blockusers_init() {
if (is_admin() && !current_user_can(\'administrator\')) {
wp_redirect(home_url());
exit;
}
}
这段代码将ajax调用重定向到主页。
要解决此问题,请添加DOING_AJAX 条件
add_action(\'init\', \'blockusers_init\');
function blockusers_init() {
if (is_admin() && !current_user_can(\'administrator\') && !defined(\'DOING_AJAX\')) {
wp_redirect(home_url());
exit;
}
}
希望这对你有帮助。