Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress AJAX returns 0 when user not logged in

This has been asked before, but I've tried a lot of stuff to make it work, and I don't know what I'm missing. I have a member's directory for a Wordpress site that uses Ajax to call and display member profiles. It also allows you to sort them alphabetically by name. Both work flawlessly when logged in, but returns '0' when logged out. I've tested it with multiple accounts with different permissions, and as long as one is logged in with any account, it works fine.

I have both

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

and

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

added, which I've read should make it work for both logged in and not logged in users. I assume that users who are not logged in are unable to access something essential to Ajax working, possibly the ajax-admin.php file I keep reading about, but any fixes I've tried do nothing.

I tried debugging in chrome (F12, check network for errors, etc) and found no helpful info. I also added console.log() lines to various parts of my code to determine whether it was being called, and they all get called as expected, logged in or out.

anyway, here's my code from functions.php (not including the add_action part):

     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);

And here's the function from my directory page:

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;

}

Apologies for the formatting on the functions.php section; it pasted weird and I tried to fix it. Does anyone have any idea what's going on?

like image 259
Grant Hendricks Avatar asked Dec 23 '22 21:12

Grant Hendricks


1 Answers

The issue is with your calls to add_action.

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

You're correct in thinking you need both wp_ajax_(action) and wp_ajax_nopriv_(action). (action) should be identical for both.

Update to:

add_action( 'wp_ajax_nopriv_load-members', 'load_members' );
add_action( 'wp_ajax_load-members', 'load_members' );
like image 105
Nathan Dawson Avatar answered Dec 26 '22 10:12

Nathan Dawson