Why WordPress admin-ajax.php calls return 0

If you need to test drive WordPress, Try it now!

Sharing is caring!

You will see that kind of error if you are developing a custom WordPress plugin that makes Ajax calls.

A normal response to an Ajax call should be a JSON object that contains some useful information.

Firefox Developer Tools Request Response

The reason could be that the action is not being handled properly. The plugin must send exactly at least the action parameter, and depending on its value, WordPress will trigger the appropriate hook.

action=wpsandbox_cool_plugin_my_ajax_action

How to properly hook for Ajax requests in WordPress

// Hook the function to handle logged in users
add_action('wp_ajax_wpsandbox_cool_plugin_my_ajax_action', 'wpsandbox_cool_plugin_name_process_ajax'');

// handle guests (non-logged in users
add_action('wp_ajax_nopriv_wpsandbox_cool_plugin_my_ajax_action', 'wpsandbox_cool_plugin_name_process_ajax'');

Sharing is caring!

If you need to test drive WordPress, Try it now!
This code shared in this post/site is provided "AS IS" without any warranties or guarantees of any kind. The authors and distributors of this code cannot be held responsible for any damage, data loss, or other issues that may arise from its use. It is strongly recommended that you test this code in a staging environment before deploying it to a live site. Always ensure you have proper backups and understand the implications of running this code on your WordPress installation. Use at your own risk.

Leave a Comment

Your email address will not be published. Required fields are marked *