wp_update_comment

Updates an existing comment in the database.

Description

(int) wp_update_comment( (array) $commentarr ); 

Filters the comment and makes sure certain fields are valid before updating.

Returns (int)

Comment was updated if value is 1, or was not updated if value is 0.

Parameters (1)

0. $commentarr (array)
Contains information on the comment.

Usage

  1. if ( !function_exists( 'wp_update_comment' ) ) { 
  2. require_once ABSPATH . WPINC . '/comment.php'; 
  3.  
  4. // Contains information on the comment. 
  5. $commentarr = array(); 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_update_comment($commentarr); 
  9.  

Defined (1)

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

/wp-includes/comment.php  
  1. function wp_update_comment($commentarr) { 
  2. global $wpdb; 
  3.  
  4. // First, get all of the original fields 
  5. $comment = get_comment($commentarr['comment_ID'], ARRAY_A); 
  6. if ( empty( $comment ) ) { 
  7. return 0; 
  8.  
  9. // Make sure that the comment post ID is valid (if specified). 
  10. if ( ! empty( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) { 
  11. return 0; 
  12.  
  13. // Escape data pulled from DB. 
  14. $comment = wp_slash($comment); 
  15.  
  16. $old_status = $comment['comment_approved']; 
  17.  
  18. // Merge old and new fields with new fields overwriting old ones. 
  19. $commentarr = array_merge($comment, $commentarr); 
  20.  
  21. $commentarr = wp_filter_comment( $commentarr ); 
  22.  
  23. // Now extract the merged array. 
  24. $data = wp_unslash( $commentarr ); 
  25.  
  26. /** 
  27. * Filters the comment content before it is updated in the database. 
  28. * @since 1.5.0 
  29. * @param string $comment_content The comment data. 
  30. */ 
  31. $data['comment_content'] = apply_filters( 'comment_save_pre', $data['comment_content'] ); 
  32.  
  33. $data['comment_date_gmt'] = get_gmt_from_date( $data['comment_date'] ); 
  34.  
  35. if ( ! isset( $data['comment_approved'] ) ) { 
  36. $data['comment_approved'] = 1; 
  37. } elseif ( 'hold' == $data['comment_approved'] ) { 
  38. $data['comment_approved'] = 0; 
  39. } elseif ( 'approve' == $data['comment_approved'] ) { 
  40. $data['comment_approved'] = 1; 
  41.  
  42. $comment_ID = $data['comment_ID']; 
  43. $comment_post_ID = $data['comment_post_ID']; 
  44.  
  45. /** 
  46. * Filters the comment data immediately before it is updated in the database. 
  47. * Note: data being passed to the filter is already unslashed. 
  48. * @since 4.7.0 
  49. * @param array $data The new, processed comment data. 
  50. * @param array $comment The old, unslashed comment data. 
  51. * @param array $commentarr The new, raw comment data. 
  52. */ 
  53. $data = apply_filters( 'wp_update_comment_data', $data, $comment, $commentarr ); 
  54.  
  55. $keys = array( 'comment_post_ID', 'comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_type', 'comment_parent', 'user_id', 'comment_agent', 'comment_author_IP' ); 
  56. $data = wp_array_slice_assoc( $data, $keys ); 
  57.  
  58. $rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) ); 
  59.  
  60. clean_comment_cache( $comment_ID ); 
  61. wp_update_comment_count( $comment_post_ID ); 
  62. /** 
  63. * Fires immediately after a comment is updated in the database. 
  64. * The hook also fires immediately before comment status transition hooks are fired. 
  65. * @since 1.2.0 
  66. * @since 4.6.0 Added the `$data` parameter. 
  67. * @param int $comment_ID The comment ID. 
  68. * @param array $data Comment data. 
  69. */ 
  70. do_action( 'edit_comment', $comment_ID, $data ); 
  71. $comment = get_comment($comment_ID); 
  72. wp_transition_comment_status($comment->comment_approved, $old_status, $comment); 
  73. return $rval;