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. :)
Specific Problems
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.
Helpful Tip
The PHP function that processes the AJAX request has to echo something and should end with:
wp_die();
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:
wp_die(print_r($some_array));