wp_set_comment_status

Sets the status of a comment.

Description

(bool|WP_Error) wp_set_comment_status( (int|WP_Comment) $comment_id, (string) $comment_status, (constant) $wp_error = false ); 

The action is called after the comment is handled. If the comment status is not in the list, then false is returned.

Returns (bool|WP_Error)

True on success, false or WP_Error on failure.

Parameters (3)

0. $comment_id (int|WP_Comment)
Comment ID or WP_Comment object.
1. $comment_status (string)
New comment status, either hold,, approve., spam, or trash.
2. $wp_error — Optional. (constant) => false
Whether to return a WP_Error object if there is a failure. Default is false.

Usage

  1. if ( !function_exists( 'wp_set_comment_status' ) ) { 
  2. require_once ABSPATH . WPINC . '/comment.php'; 
  3.  
  4. // Comment ID or WP_Comment object. 
  5. $comment_id = null; 
  6.  
  7. // New comment status, either 'hold', 'approve', 'spam', or 'trash'. 
  8. $comment_status = ''; 
  9.  
  10. // Whether to return a WP_Error object if there is a failure. Default is false. 
  11. $wp_error = false; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = wp_set_comment_status($comment_id, $comment_status, $wp_error); 
  15.  

Defined (1)

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

/wp-includes/comment.php  
  1. function wp_set_comment_status($comment_id, $comment_status, $wp_error = false) { 
  2. global $wpdb; 
  3.  
  4. switch ( $comment_status ) { 
  5. case 'hold': 
  6. case '0': 
  7. $status = '0'; 
  8. break; 
  9. case 'approve': 
  10. case '1': 
  11. $status = '1'; 
  12. break; 
  13. case 'spam': 
  14. $status = 'spam'; 
  15. break; 
  16. case 'trash': 
  17. $status = 'trash'; 
  18. break; 
  19. default: 
  20. return false; 
  21.  
  22. $comment_old = clone get_comment($comment_id); 
  23.  
  24. if ( !$wpdb->update( $wpdb->comments, array('comment_approved' => $status), array( 'comment_ID' => $comment_old->comment_ID ) ) ) { 
  25. if ( $wp_error ) 
  26. return new WP_Error('db_update_error', __('Could not update comment status'), $wpdb->last_error); 
  27. else 
  28. return false; 
  29.  
  30. clean_comment_cache( $comment_old->comment_ID ); 
  31.  
  32. $comment = get_comment( $comment_old->comment_ID ); 
  33.  
  34. /** 
  35. * Fires immediately before transitioning a comment's status from one to another 
  36. * in the database. 
  37. * @since 1.5.0 
  38. * @param int $comment_id Comment ID. 
  39. * @param string|bool $comment_status Current comment status. Possible values include 
  40. * 'hold', 'approve', 'spam', 'trash', or false. 
  41. */ 
  42. do_action( 'wp_set_comment_status', $comment->comment_ID, $comment_status ); 
  43.  
  44. wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment); 
  45.  
  46. wp_update_comment_count($comment->comment_post_ID); 
  47.  
  48. return true;