2017-06-20 125 views
1

對於我通過這個被要求每隔一次搜查,但未能找到解決wp_ajax_nopriv不工作的註銷用戶

我只是想AJAX內容添加到頁面註銷用戶和失敗的記錄使用wp_ajax_nopriv喘息。我嘗試停用所有插件,但沒有幫助。儘管我確實想要將後端管理員的訪問權限鎖定到管理員,但我目前沒有實現。

加載腳本

function html5blank_header_scripts() 
{ 
    if ($GLOBALS['pagenow'] != 'wp-login.php' && !is_admin()) { 

     wp_register_script('globalJs', get_template_directory_uri() . '/js/global.js', array('jquery'), '1.0.0'); // Custom scripts 
     wp_enqueue_script('globalJs'); // Enqueue it! 

     wp_localize_script(
      'globalJs', 
      'ajax_object', 
      array('ajax_url' => admin_url('admin-ajax.php'), 'we_value' => 1234) 
     ); 
    } 
} 

PHP AJAX功能

add_action('wp_ajax_new_ajax_event_template', 'new_ajax_event_template'); 
add_action('wp_ajax_nopriv_new_ajax_event_template', 'new_ajax_event_template'); 

function new_ajax_event_template() { 
    global $post; 
    $postID = $_POST["post_id"]; 
    $post = get_post($postID); 
    <------- lots of stuff that works for logged in user -----> 
    die(); 
} 

JQuery的AJAX腳本

$(document).on("click", ".title[data-ajax='post']", function() { 
    $post_id = $(this).data("id"); 
    $post_type = $(this).data("type"); 
    $post_url = $(this).data("url"); 
    var historyData = $(".wrapper").attr("data-history"); 
    if (!historyData){ 
     historyData = []; 
    } 
    else{ 
     historyData = JSON.parse(historyData); 
    } 
    historyData.push({ 
     id: $post_id, 
     type: $post_type, 
     url: $post_url 
    }); 
    var data = { 
     action : 'new_ajax_'+ $post_type +'_template', 
     post_id : $post_id 
    }; 
    $("#main").empty().hide(); 
    $.ajax({ 
     url : ajax_object.ajax_url, 
     type : 'post', 
     data : data, 
     success: function(html) { 
      $("#main").append(html); 
      history.pushState('data', '', $post_url); 
      $(".wrapper").attr("data-id", $post_id); 
      $(".wrapper").attr("data-history", JSON.stringify(historyData)); 
      $("#main").fadeIn("fast"); 
     } 
    }); 
}); 

這一切完全適用於已登錄的用戶,但無法爲登錄用戶和我不知道爲什麼。 functions.php中沒有任何內容可以防止這種情況發生。

回答

0

我通過確保管理員-ajax.php固定的,這是建立在頭

add_action('wp_head', 'plugin_set_ajax_url'); 
function plugin_set_ajax_url() { 
?> 
    <script type="text/javascript"> 
     var ajax_object = {}; 
     ajax_object.ajax_url = '<?php echo admin_url('admin-ajax.php'); ?>'; 
    </script> 
<?php 
} 
1

檢查以下的情況下,我不能說肯定

1)你有沒有得到瀏覽器控制檯?檢查jQuery的錯誤,如果有什麼明確的。

2)檢查瀏覽器開發人員工具管理-ajax.php響應 - >網絡(PFA)

enter image description here