Still failing to figure out what I’m doing wrong about making a PHP function handle an AJAX request in WordPress

After literally hours of trying to hook up a function to handler AJAX requests through WordPress’s API, I am still coming short. I’ve gone back to basic by trying a basic test

<?php
    add_action( 'wp_ajax_member_update', 'member_update' );

    function member_update ( )
    {
        echo $_POST['testvariable'];
    }
?>

<script type="text/javascript">
    jQuery(document).ready(function($) {

        var data = {
            'action': 'member_update',
            'testvariable': 1234
        };

        $.post(ajaxurl, data, function(response) {
            alert('Got this from the server: ' + response); // expected: 1234
        });
    });
</script>

and even this is returning Status Code 200 and Response 0. What am I missing? I’m perfectly following the documentation as far as I can tell.

Related posts

1 comment

  1. If you are setting your add_action code in your page, this action won’t be called since you are using ajaxurl and it usually goes to wp-load.php. You should define your action in your theme’s functions.php code and that will make it be available all over the site (as well as for AJAX requests).

Comments are closed.