0

I'm trying to run a function when deleting a comment. This entails hooking into "delete_comment" if I'm not mistaken.

Theoretically, this should work as a plugin, so when you delete a comment, it emails me. But when I install and activate it, nothing happens when I delete a comment. I hard coded the email function into ajax-actions.php and it works, but I think the idea is to have all the functions in the plugin file if possible. What am I missing here? Am I missing a do_action somewhere?

add_action( 'delete_comment',  'do_something', 10 ,1);  

function do_something($comment_id)  {
    mail("[email protected]", "$comment_id","$comment_id");
}

Note: If I require() the plugin file and call do_something() from /wp-admin/includes/ajax-actions.php, it works...

function wp_ajax_delete_comment() {
    ...
    if ( isset($_POST['trash']) && 1 == $_POST['trash'] ) {
        if ( 'trash' == $status )
            wp_die( time() );

        // BEGIN INSERT
        require_once( plugin-file.php' );
        do_something($comment->comment_ID); // THIS WORKS FINE
        // END INSERT

        $r = wp_trash_comment( $comment->comment_ID );
    }
    comment_to_post($comment->comment_ID);
    ...
}

Is wp-admin/includes/ajax-actions.php NOT a part of the regular WP install and not in the scope that you can hook into?

4
  • Are you sure the mail() function is working on your server in the first place? Try doing something else inside that function, like update_option( 'test_option', $comment_id );
    – EAMann
    Oct 23, 2012 at 19:34
  • 3
    Try using wp_mail() instead. Try running a simple test on wp_mail first to see if you receive any email. You want to determine its actually working. If not, you may need to specify your mail server settings in the php.ini file. But I suspect the latter is not your problem.
    – Adam
    Oct 23, 2012 at 19:46
  • Tried wp_mail() and still does nothing. php's mail() works fine and mail server is up. But I'm not specifically trying to mail, I'm just trying to verify that the plug-in is running do_something(). So far, no dice. Is it safe to assume that the code above SHOULD work, or am I missing something basic?
    – vanarie
    Oct 23, 2012 at 20:13
  • The correct file is wp-admin/admin-ajax.php. Your own AJAX file doesn’t run in a WordPress context probably.
    – fuxia
    Oct 23, 2012 at 20:21

2 Answers 2

1

Unless you're passing $force_delete as true when calling wp_delete_comment(), the delete_comment hook doesn't fire. This is because WordPress calls wp_trash_comment() at the beginning of wp_delete_comment() if $force_delete isn't set true.

Try hooking into the trash_comment action hook:

function _dbdb_delete_comment( $comment_id ){
    $filter = current_filter();
    var_dump( $filter );
    var_dump( $comment_id );
    wp_die( __FUNCTION__ );
 }

add_action('delete_comment', '_dbdb_delete_comment' );
add_action('trash_comment', '_dbdb_delete_comment' );

I added that to my theme's functions file and checked my network response tab in Chrome Dev tools. I tested by deleting (trashing) a comment through the Comments screen in the Admin. It worked as expected.

The filter that was returned was trash_comment.

0

I would have just added as a comment to this, but I can't comment...

A few things I would try are:

Make sure that you're actually getting into the "do_something" function by outputting to debug or the screen.

Another thing is to set the priority (10 in this case) to something low so that this is executed first.

Lastly, this might be getting trashed first in which case you can try the trash_comment hook and see if that works.

I hope this helps!

4
  • I updated the question a little to clarify about manually calling it from /wp-admin/includes/ajax-actions.php - which works fine. I just can't seem to get the hook to work from outside of it. Any other suggestions to try?
    – vanarie
    Oct 23, 2012 at 23:38
  • After some digging, I found a few websites that may point me in the right direction, such as w4dev.com/wp/wp_ajax & core.svn.wordpress.org/tags/3.4.2/wp-admin/admin-ajax.php. Seems that I might be missing the "wp_ajax_" prefix in the first parameter of add_action()
    – vanarie
    Oct 24, 2012 at 4:23
  • But then you wouldn't be hooking into the delete_comment action. When you do a add_action, you're basically telling WP to do something when delete_comment happens. If you go the ajax route, then you would have to have some code on the front end that handles when the delete comment link is clicked. This handler would then make an ajax request to an action that you specify. This is where the wp_ajax_your_action would work. Does this make more sense?
    – dmarges
    Oct 24, 2012 at 14:15
  • Yes, I think so. By your answer, I assume that if I turned ajax off, my code would work fine? Funny thing is, I've been searching the web for any examples of hooking to the comment_delete action, and there doesn't seem to be much to look at. Rather surprising actually, but I might be a special case. Would you show me a small example in this case?
    – vanarie
    Oct 24, 2012 at 15:14

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.