WordPress admin AJAX request always return 0 even with die()

Advertisement

WordPress offers a great way for theme and plugin developers to add functionality using AJAX and with the use of their API your theme and plugin works smoothly but wait what this AJAX mean? AJAX is stands for Asynchronous JavaScript and XML and AJAX is not a new programming language, but a new way to use existing standards, okay so much for that.

So you’re working in Worpress AJAX huh? Last week I’m working on my plugin and I’m using AJAX on it, all fine the code, the callback function, everything and I began to scratch my head, why I only got zero response? Luck for you mate I will share you the fix.

Solution 1

Make sure that you’re calling the action properly in your PHP file, a sample action declaration inside in your JS file.

JS File


Function ...(){
            var data = {
                action: 'ACTION',
                field_name: "value"
            };
            jQuery.post(ajaxurl, data, function(response) {
               alert(response);
            });
        }

PHP File

To add the action please see below, notice the word ACTION after wp_ajax_ and wp_ajax_nopriv


add_action( 'wp_ajax_ACTION', 'your_callback_function' );
add_action( 'wp_ajax_nopriv_ACTION', 'your_callback_function' );

That mean we allow with privilege or no privilege users.

Solution 2

This is the most common mistakes ever; please don’t forgot to include the PHP file in your plugin or theme init 😉

Advertisement