wp_delete_comment

Trashes or deletes a comment.

Description

wp_delete_comment( (int|WP_Comment) $comment_id, (constant) $force_delete = false ); 

The comment is moved to trash instead of permanently deleted unless trash is disabled, item is already in the trash, or $force_delete is true.

The post comment count will be updated if the comment was approved and has a post ID available.

Parameters (2)

0. $comment_id (int|WP_Comment)
Comment ID or WP_Comment object.
1. $force_delete — Optional. (constant) => false
Whether to bypass trash and force deletion. Default is false.

Usage

  1. if ( !function_exists( 'wp_delete_comment' ) ) { 
  2. require_once ABSPATH . WPINC . '/comment.php'; 
  3.  
  4. // Comment ID or WP_Comment object. 
  5. $comment_id = null; 
  6.  
  7. // Whether to bypass trash and force deletion. Default is false. 
  8. $force_delete = false; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_delete_comment($comment_id, $force_delete); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/comment.php  
  1. function wp_delete_comment($comment_id, $force_delete = false) { 
  2. global $wpdb; 
  3. if (!$comment = get_comment($comment_id)) 
  4. return false; 
  5.  
  6. if ( !$force_delete && EMPTY_TRASH_DAYS && !in_array( wp_get_comment_status( $comment ), array( 'trash', 'spam' ) ) ) 
  7. return wp_trash_comment($comment_id); 
  8.  
  9. /** 
  10. * Fires immediately before a comment is deleted from the database. 
  11. * @since 1.2.0 
  12. * @param int $comment_id The comment ID. 
  13. */ 
  14. do_action( 'delete_comment', $comment->comment_ID ); 
  15.  
  16. // Move children up a level. 
  17. $children = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment->comment_ID) ); 
  18. if ( !empty($children) ) { 
  19. $wpdb->update($wpdb->comments, array('comment_parent' => $comment->comment_parent), array('comment_parent' => $comment->comment_ID)); 
  20. clean_comment_cache($children); 
  21.  
  22. // Delete metadata 
  23. $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d", $comment->comment_ID ) ); 
  24. foreach ( $meta_ids as $mid ) 
  25. delete_metadata_by_mid( 'comment', $mid ); 
  26.  
  27. if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment->comment_ID ) ) ) 
  28. return false; 
  29.  
  30. /** 
  31. * Fires immediately after a comment is deleted from the database. 
  32. * @since 2.9.0 
  33. * @param int $comment_id The comment ID. 
  34. */ 
  35. do_action( 'deleted_comment', $comment->comment_ID ); 
  36.  
  37. $post_id = $comment->comment_post_ID; 
  38. if ( $post_id && $comment->comment_approved == 1 ) 
  39.  
  40. clean_comment_cache( $comment->comment_ID ); 
  41.  
  42. /** This action is documented in wp-includes/comment.php */ 
  43. do_action( 'wp_set_comment_status', $comment->comment_ID, 'delete' ); 
  44.  
  45. wp_transition_comment_status('delete', $comment->comment_approved, $comment); 
  46. return true;