Wordpress Admin Ajax call


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:

// 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;

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

            case 'delete_post':

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

<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