1

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:

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.

9
  • What's your PHP version?
    – wyrfel
    Feb 23, 2011 at 6:14
  • I use PHP 5.3.1 (XAMPP 1.7.3 Lite)
    – Anh Tran
    Feb 23, 2011 at 9:04
  • What is shown inside the alert? Nothing? That's normal, your ajax callback isn't giving any data back, echo a simple string inside the callback before the die statement and you'll see it in the response.
    – t31os
    Feb 23, 2011 at 12:38
  • Yes, nothing is echoed in the alert. But when I even echo something in delete_image() function, the alert function still echoes nothing, too. Btw, I noticed that the script doesn't work because the image wasn't deleted.
    – Anh Tran
    Feb 23, 2011 at 14:21
  • 2
    Why aren't you sending 'attach_id' as POST data in your AJAX call? Sep 21, 2011 at 4:58

3 Answers 3

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.

1
  • 1
    Yes, of course. I didn't write in the code above, but I had created an instance of class.
    – Anh Tran
    Feb 23, 2011 at 6:03
1

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.

1
  • 1
    Yes, it should work as I expected. Now it works in my updated script. But I still haven't found the error. Maybe I did something wrong elsewhere or there was a stupid error such as syntax error. Anyway, thanks for your comment and for the debugger!
    – Anh Tran
    May 14, 2011 at 7:30
0

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}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.