I was working with some AJAX functionality a while back and started writing down the problems and solutions I ran across so they were all in one place. Basically, this post is for my future self. I hope you find it useful as well. :)
Function Always Returns 0
If you’re getting a “0” as the response from an AJAX call in the admin, it’s most likely one of three things:
- WordPress can’t find the function you’ve hooked.
- You don’t have a wp_die() at the end of the PHP function.
- The action isn’t in the data you’re sending to the PHP function.
403 Forbidden Error
Check the Nonce. You probably copied it from another function and it’s checking the wrong one.
The PHP function that processes the AJAX request has to echo something and should end with:
You can use that to return something you want to see, like what data was passed to the function.
To check an array, use this instead: