WordPress wp_ajax returning 0 error

This morning I spent a little time constructing an ajax call from a front-end page (not an admin page) using wp_ajax. As is my wont, I tested this in Firefox and all was well. I used the WP codex.

Then I tried it in Opera and it failed, no response whatsoever, just a 0 in the ajax response. Seemed odd that Opera should have a problem, tried it in Chrome, same thing. Lots of Googling later, saw loads of similar questions being asked, no answers…

I then looked at the code in wp_ajax, and all became clear…

I had entries in my functions.php as follows:

 add_action('wp_ajax_myfunction', 'myfunction');
 
 function myfunction() {
    //stuff
 }

So if I pass an action of ‘myfunction’ to wp_ajax it should call the hook at the top of the above code, which in turn calls myfunction(), right? Only if you are logged on to WordPress at the time! If you aren’t logged in, wp_ajax calls the hook wp_ajax_nopriv_myfunction. And that’s why it worked for me in Firefox, because I was also logged on through Firefox. Nowhere is this mentioned in the codex, that I can see.

Having identified this it’s then simple to add another hook to your functions.php:

 add_action('wp_ajax_nopriv_myfunction', 'myfunction');

Hope this saves someone an hour or so…

Tags:

19 responses to “WordPress wp_ajax returning 0 error”

  1. Kiran A says:

    Thank you!

  2. Nitin Suresh says:

    Thank You man. Saved me hours.

  3. Simon says:

    Impossible to guess what might be the problem. Since this is largely server side code browser variations are not likely. Start with IE dev tools. look for any errors there. Just possible there might be a js error somewhere else on the page that’s preventing the ajax call I suppose.

  4. Rajib says:

    I have same type of problem. but it is working all browser without IE. Can you please help me?

  5. Pete says:

    Saved me an hour or ten. Thanks!

  6. Aleksey says:

    Thank you, Simon! It really helped me a lot!
    More information on the subject can be found here:
    http://codex.wordpress.org/AJAX_in_Plugins

  7. Mohamed says:

    I was curious as changing the callback functions name never triggered an error. This really helped otherwise a long days work.

    Thanks! 🙂

  8. Santiago says:

    Thank you so much!!!! that is wondefull. I would not have guessed that one. I don’t know why WP doens’t provide this information in the documentation. It solved my problem as well.

  9. Sam Thomas says:

    Absolute legend. Saved me a lot of messing around. Thanks!

  10. Ravi Gohil says:

    Thank you very much……man!!!!
    I trying to solve this front ajax response probelm……from about some
    hour….
    BY using this function my problem has been solved!!!!!
    Thanks “””Dude””” Again!!!!!

  11. Thanks for the reminder! Ha. You saved me some time on this one.

  12. Peter Oravec says:

    Thank you so much!
    You save me a lot of time 😀

  13. Sajax says:

    Oh, man, thank you so much! I was getting desperate already.

  14. Gustavo says:

    Thanks man, this helps a lot!

  15. Ashok Rane says:

    This worked for me. Adding _nopriv_ is what was missing in my ajax call. Thank you.

  16. Joel says:

    Thank You! it works 🙂

  17. djbokka says:

    My God, thank you so much. I was nearly postal.

  18. Valery says:

    Thank you! Thank you!

  19. Aaron says:

    Oh man – this post certainly saved me some time. I thought it was because some crazy client was using IE!

    Much appreciated.

Useful? Interesting? Leave me a comment

I've yet to find a way of allowing code snippets to be pasted into Wordpress comments - so if you're trying to do this you'd be better off using the contact form.