wp_transition_comment_status

Call hooks for when a comment status transition occurs.

Description

wp_transition_comment_status( (string) $new_status, (string) $old_status, (object) $comment ); 

Calls hooks for comment status transitions. If the new comment status is not the same as the previous comment status, then two hooks will be ran, the first is with new status, old status, and comment data. The next action called is . It has the comment data.

The final action will run whether or not the comment statuses are the same. The action is named .

Parameters (3)

0. $new_status (string)
New comment status.
1. $old_status (string)
Previous comment status.
2. $comment (object)
Comment data.

Usage

  1. if ( !function_exists( 'wp_transition_comment_status' ) ) { 
  2. require_once ABSPATH . WPINC . '/comment.php'; 
  3.  
  4. // New comment status. 
  5. $new_status = ''; 
  6.  
  7. // Previous comment status. 
  8. $old_status = ''; 
  9.  
  10. // Comment data. 
  11. $comment = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = wp_transition_comment_status($new_status, $old_status, $comment); 
  15.  

Defined (1)

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

/wp-includes/comment.php  
  1. function wp_transition_comment_status($new_status, $old_status, $comment) { 
  2. /** 
  3. * Translate raw statuses to human readable formats for the hooks. 
  4. * This is not a complete list of comment status, it's only the ones 
  5. * that need to be renamed 
  6. */ 
  7. $comment_statuses = array( 
  8. 0 => 'unapproved',  
  9. 'hold' => 'unapproved', // wp_set_comment_status() uses "hold" 
  10. 1 => 'approved',  
  11. 'approve' => 'approved', // wp_set_comment_status() uses "approve" 
  12. ); 
  13. if ( isset($comment_statuses[$new_status]) ) $new_status = $comment_statuses[$new_status]; 
  14. if ( isset($comment_statuses[$old_status]) ) $old_status = $comment_statuses[$old_status]; 
  15.  
  16. // Call the hooks 
  17. if ( $new_status != $old_status ) { 
  18. /** 
  19. * Fires when the comment status is in transition. 
  20. * @since 2.7.0 
  21. * @param int|string $new_status The new comment status. 
  22. * @param int|string $old_status The old comment status. 
  23. * @param object $comment The comment data. 
  24. */ 
  25. do_action( 'transition_comment_status', $new_status, $old_status, $comment ); 
  26. /** 
  27. * Fires when the comment status is in transition from one specific status to another. 
  28. * The dynamic portions of the hook name, `$old_status`, and `$new_status`,  
  29. * refer to the old and new comment statuses, respectively. 
  30. * @since 2.7.0 
  31. * @param WP_Comment $comment Comment object. 
  32. */ 
  33. do_action( "comment_{$old_status}_to_{$new_status}", $comment ); 
  34. /** 
  35. * Fires when the status of a specific comment type is in transition. 
  36. * The dynamic portions of the hook name, `$new_status`, and `$comment->comment_type`,  
  37. * refer to the new comment status, and the type of comment, respectively. 
  38. * Typical comment types include an empty string (standard comment), 'pingback',  
  39. * or 'trackback'. 
  40. * @since 2.7.0 
  41. * @param int $comment_ID The comment ID. 
  42. * @param WP_Comment $comment Comment object. 
  43. */ 
  44. do_action( "comment_{$new_status}_{$comment->comment_type}", $comment->comment_ID, $comment );