Ajax with OOP doesn’t work

I have a small script within theme’s functions.php file that uses ajax, principle like this:

add_action('admin_head', 'rw_script');
function rw_script() {
    echo '
    <script type="text/javascript">
        // delete image
        $(".delete_image").click(function(){
            var data = $(this).attr("rel");
            $.post(
                ajaxurl,
                {action: 'rw_delete_image', data: data}, function(response){
                    alert(response); // debug
                }
            );

            return false;
        });
    });
    </script>';
}
function delete_image() {
    if (isset($_POST['attach_id'])) wp_delete_attachment($_POST['attach_id']);
        die();
}
add_action('wp_ajax_rw_delete_image', 'delete_image');

It works fine. But when I turned it into OOP, the ajax doens’t work:

Read More
class RW_Test {
    function __construct() {
        add_action('admin_head', array(&$this, 'rw_script'));
        add_action('wp_ajax_rw_delete_image', array(&$this, 'delete_image'));
    }
    function rw_script() {
        echo '
        <script type="text/javascript">
            // delete image
            $(".delete_image").click(function(){
                var data = $(this).attr("rel");
                $.post(
                    ajaxurl,
                    {action: 'rw_delete_image', data: data}, function(response){
                        alert(response); // debug
                    }
                );

                return false;
            });
        });
        </script>';
    }
    function delete_image() {
        if (isset($_POST['attach_id'])) wp_delete_attachment($_POST['attach_id']);
            die();
    }
}

The javascript works fine, i.e when I click on delete links, data is sent properly. But the action for deleting image isn’t fired.

Do you know how to solve this? Please help me. Thank you.

Related posts

Leave a Reply

3 comments

  1. You need to create a new instance of the class so that your constructor is called and your actions are registered. e.g. you need to add something like $test = new PR_Test; below the definition of PR_Test.

  2. It should work; I’ve done the exact same thing successfully in a couple different styles of custom post type classes. There’s a possibility that your original RW_Test object is being overwritten—perhaps you’re using the same variable name somewhere else?

    At any rate, a good place to start troubleshooting is to look at all the active hooks to make sure that your “wp_ajax_rw_delete_image” action is active (and paired with the correct RW_Test object) when you initiate the AJAX call. I suggest this hooks debugger from Rarst to echo the active hooks in your admin page.

  3. It’s been a long time since this question, now my code has changed a lot. But after re-look at the code, and I think the request should be:

    $.post(
        ajaxurl,
        {action: 'rw_delete_image', attach_id: data}, function(response){
             alert(response); // debug
        }
    );
    

    not {action: 'rw_delete_image', data: data}.