2017-01-10 32 views
1

這已被問過,但我已經嘗試了很多東西以使其工作,並且我不知道我錯過了什麼。我有一個使用Ajax調用和顯示成員配置文件的Wordpress網站的成員目錄。它還允許您按名稱按字母順序排序。登錄時都可以完美工作,但退出時返回'0'。我已經使用不同權限的多個帳戶對其進行了測試,只要有人使用任何帳戶登錄,就可以正常工作。當用戶未登錄時,Wordpress AJAX返回0

我都

add_action('wp_ajax_nopriv_load-filter', 'load_members'); 

add_action('wp_ajax_load-members', 'load_members'); 

補充說,這是我讀過應該讓它在這兩個記錄,並在用戶未登錄工作。我假設沒有登錄的用戶無法訪問Ajax工作必不可少的東西,可能是我一直在閱讀的ajax-admin.php文件,但是我嘗試過的任何修補程序都無能爲力。

我試着在chrome中調試(F12,檢查網絡錯誤等),發現沒有有用的信息。我還將console.log()行添加到代碼的各個部分,以確定它是否被調用,並且它們都按預期調用,登錄或退出。

無論如何,這裏是從的functions.php(不包括ADD_ACTION部分)我的代碼:

 function load_members() 
      { 

       $letter = $_POST[ 'letter' ]; 

        if($letter == "all") { 

         $args = array(
          'role' => 'Subscriber',); 

          $user_query = new WP_User_Query($args); 

          ob_start(); 

          global $user; 

          if (!empty($user_query->results)) { 
          foreach ($user_query->results as $user) { 
          get_template_part('template', 'directory'); 
         } 
           } else { 
       echo '<div class="center" >No users found.</div>';} 
       $response = ob_get_contents(); 
        ob_end_clean(); 

        echo $response; 

        die(1); 

在這裏,從我的目錄頁面的功能:

function members_get(catID) { 
jQuery("a.ajax").removeClass("current"); 
jQuery("#category-post-content").fadeOut(); 
$('.' + catID).addClass("current"); //adds class current to the menu item being displayed so you can style it with css 
jQuery("#loading-animation").show(); 
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); //must echo it?>'; 
console.log(ajaxurl); 
jQuery.ajax({ 
type: 'POST', 
url: ajaxurl, 
data: {"action": "load-members", letter: catID }, 
success: function(response) { 
jQuery("#category-post-content").fadeIn(); 
jQuery("#category-post-content").html(response); 
jQuery("#loading-animation").hide(); 
return false; 

} 

道歉的格式functions.php部分;它粘貼奇怪,我試圖修復它。有誰知道發生了什麼事?

回答

5

問題在於您撥打add_action

add_action('wp_ajax_nopriv_load-filter', 'load_members'); 
add_action('wp_ajax_load-members', 'load_members'); 

你在想你需要同時wp_ajax_(action)wp_ajax_nopriv_(action)正確的。 (action)應該對於兩者都是相同的。

更新到:

add_action('wp_ajax_nopriv_load-members', 'load_members'); 
add_action('wp_ajax_load-members', 'load_members'); 
+1

謝謝,這是問題。花了很長時間來解決這樣一個小問題! –