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.