Wordpress Admin Ajax call

admin

Administrator
Staff member
I'm trying to do a simple dashboard widget where the user can see posts with status 'pending' and from there either approve or remove them. For this I obviously need to do an ajax call and use hooks to work it into WP. This is what I've got so far:

Code:
// admin area ajax function 
add_action('admin_head', 'my_action_javascript');

function my_action_javascript($action) {
    ?>
    <script type="text/javascript" >
    jQuery(document).ready(function($) {
        $('.controls a').on('click', function() {
            var $this = $(this),
                data = {
                    action: $this.attr('class'),
                    postid: $this.attr('id')        
                };  
            jQuery.post(ajaxurl, data, function(response) {
            alert('Got this from the server: ' + response);
            });
            return false;
        });
     });
     </script>
     <?php
}

add_action('wp_ajax_my_action', 'my_action_callback');

function my_action_callback() {
    $action = $_POST['action'];
    $postid = $_POST['postid'];

    switch($action) {
            case 'approve_post':
                $mypost = array();
                $mypost['ID'] = $postid;
                $mypost['post_status'] = 'publish';
                wp_update_post($mypost);
            break;

            case 'delete_post':
                wp_delete_post($postid);
            break;
     }
     die();
}

Code is taken from the example from the wordpress codex.
The links i refer to in my jQuery looks like this:

Code:
<a href="#" id="'.get_the_ID().'" class="delete_post">Delete</a>

I've checked in the click function that the id and class transfered is correct.

The response i get from the ajax call is '0', which according to the codex means it was successful, but nothing happens. The post doesn't get deleted when I press delete, and it doesn't change post_status when I click approve.

What am I doing wrong here?

// Cheers