Why WordPress admin-ajax.php calls return 0

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'');

Subscribe to our newsletter

Sharing is caring!

Leave a Comment

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

Free Online Store by Go359.com
The new new server is up and running. Enjoy!