Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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?

share|improve this question
add comment

2 Answers 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.

share|improve this answer
    
thanks Roman, I think that's what I'll have to do in the future if it turns up again. Unfortunately, in this case I don't have access to the users files and now I've forgotten completely who has having the problem! –  CommentLuv Sep 8 '11 at 9:39
add comment

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.

share|improve this answer
    
I know the action is detected because the logged in user action fires for logged in users –  CommentLuv Jul 5 '11 at 13:07
    
I tried this and all vars are present and correct –  CommentLuv Jul 5 '11 at 13:15
    
@CommentLuv hmm... I am at loss then. Could you maybe share actual code? Simplified, just skeleton of what it going on. –  Rarst Jul 5 '11 at 13:45
    
you can see the whole thing in the commentluv plugin wordpress.org/extend/plugins/commentluv/ it's ok, I've given up now. no errors in log and it's an isolated case . I'll give it more time if someone else reports it –  CommentLuv Jul 5 '11 at 20:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.