deleted_comment
Fires immediately after a comment is deleted from the database (before the cache is cleared).
After the hook fires, wp_delete_comment() performs the following actions:
-
Updates (decreases by 1) the post's comment count — wp_update_comment_count()
-
Deletes the comment object cache — clean_comment_cache()
-
Fires the wp_set_comment_status hook
- Changes the status to
delete— wp_transition_comment_status()
The hook fires only when deleting from the Trash (permanent deletion). If a comment is first moved to the Trash, this hook will not fire! The similar trashed_comment hook fires when a comment is moved to the Trash.
Usage
add_action( 'deleted_comment', 'wp_kama_deleted_comment_action', 10, 2 );
/**
* Function for `deleted_comment` action-hook.
*
* @param string $comment_id The comment ID as a numeric string.
* @param WP_Comment $comment The deleted comment.
*
* @return void
*/
function wp_kama_deleted_comment_action( $comment_id, $comment ){
// action...
}
- $comment_id(int)
- The comment ID.
- $comment(WP_Comment)
- The deleted comment object.
Examples
#1 Send an email to the author of a deleted comment
add_action( 'deleted_comment', 'send_email_commentator_after_deletion' );
function send_email_commentator_after_deletion( $comment_id ) {
$email = get_comment_author_email( $comment_id );
$subject = 'Your comment was deleted';
$message = 'For some reason, your comment was deleted.';
wp_mail( $email, $subject, $message );
}Changelog
| Since 2.9.0 | Introduced. |
| Since 4.9.0 | Added the $comment parameter. |
Where the hook is called
wp-includes/comment.php 1615
do_action( 'deleted_comment', $comment->comment_ID, $comment );