WordPress Admin Ajax Response 0

I’ve created a plugin in WordPress which is working fine in a single file. Now I need to have an ajax type of functionality where the main plugin file will call a function on another file on user click and get the query run over there n return the result.

I tried the admin ajax code below where currently I placed the jquery and php code put on the main file at the moment to see if all works fine. But after placing the codes and registered them by add function, I always get 0 in return. I also tried different solutions online but none worked and it always returns 0.

Read More

I think I’m making some mistake in using the right code on right file maybe.

Any help would be appreciated.

Heres the code:

<?
function add_js_to_wp_footer(){ ?>
    <script type="text/javascript">

jQuery(document).ready(function($) {

    var data = {
        action: 'my_action'
    };

    jQuery.post(ajaxurl, data, function(response) {
        alert('Got this from the server: ' + response);
    });
});

    </script>
<?

}
// Then, set up a PHP function that will handle that request:

function my_action_callback() {
    global $wpdb; // this is how you get access to the database
    echo "zkhan ";

    die(); // this is required to return a proper result
}

add_action( 'admin_footer', 'add_js_to_wp_footer' );
add_action('wp_ajax_my_action', 'my_action_callback');
?>

Related posts

Leave a Reply