Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
What's your PHP version? –  wyrfel Feb 23 '11 at 6:14
    
I use PHP 5.3.1 (XAMPP 1.7.3 Lite) –  Rilwis Feb 23 '11 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 '11 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. –  Rilwis Feb 23 '11 at 14:21
2  
Why aren't you sending 'attach_id' as POST data in your AJAX call? –  Brian Fegter Sep 21 '11 at 4:58
show 4 more comments

3 Answers

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.

share|improve this answer
1  
Yes, of course. I didn't write in the code above, but I had created an instance of class. –  Rilwis Feb 23 '11 at 6:03
add comment

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.

share|improve this answer
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! –  Rilwis May 14 '11 at 7:30
add comment
up vote 0 down vote accepted

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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