wp_ajax_nopriv_xxx is not firing on one site, works on all others. -1 for logged out users

my plugin uses add_action for wp_ajax_cl_ajax and wp_ajax_nopriv_cl_ajax in it’s code and on the few thousand sites that are using it, it is fine but one site is not working for logged out users

a logged in user is fine, the code sends a request to ajax and everything gets returned fine.

Read More

on other blogs, a logged out user is fine but just on this one blog a -1 gets returned for the ajax request.

I have added debug code to the plugin to die at the do_ajax function with a message but it doesn’t even get to that function.

here’s the action for the logged out user ajax
add_action('wp_ajax_nopriv_cl_ajax','do_ajax');

the function has this

function do_ajax(){
    if(isset($_REQUEST['debug'])){
      die('in ajax function');
   }
}

I just change the ajax url to have a var for debug (…./wp-admin/admin-ajax.php?debug=true)
on my test sites it dies, for a logged in user it dies but for a logged out user, it just returns -1 which tells me the action is not even being triggered. I added more debug code to the function that adds the actions for has_filter and it returns 1 so i know the action is added, just not fired.

I’ve tried changing the theme but no joy there

the site is using wp 3.2

does anyone have any experience with wp_ajax_nopriv_xxx not firing? what caused it for you?

as I said, it is working fine on other blogs so I know it isn’t a case of a typo or missing code.

what other things can I do to determine what is causing the action not to fire?

Related posts

Leave a Reply

2 comments

  1. The condition for such hook to fire is quite simple, as per admin-ajax.php:

    if ( !empty( $_REQUEST['action'] ) )
        do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
    

    So the likely reasons are:

    1. You script does not pass action name properly (which is unlikely if exactly same code works for logged in user).
    2. Action name gets messed up in WordPress by something and it somehow depends on user being logged in.

    I’d try to hook into admin_init and dump all of $_REQUEST.

  2. I don’t think that your wp_ajax_nopriv action isn’t registered correctly. It’s just declined from admin-ajax.php. I had a similar error with the wp_ajax action handler, which returned -1 for the user role contributer.

    The problem was that I had restricted the access to the admin backend for this user group on the admin_init action hook. As you can see in admin-ajax.php this hook will be called in the first few lines, so the request was declined there.

    Maybe you have a similar problem or have activated some fancy plugins which will interfere at some point in the code.

    If you could not reproduce the error, try to go through the admin-ajax.php file and replace step by step all -1 return values with your own error message. Its a bit cumbersome but will let you find the reason.