get_comment

Retrieves comment data given a comment ID or comment object.

Description

(WP_Comment|array|null) get_comment( (constant) &$comment = null, (string) $output = ); 

If an object is passed then the comment data will be cached and then returned after being passed through a filter. If the comment is empty, then the global comment variable will be used, if it is set.

Returns (WP_Comment|array|null)

Depends on $output value.

Parameters (2)

0. $comment — Optional. (constant) => &$comment = null
Comment to retrieve.
1. $output — Optional. (string)
The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Comment object, an associative array, or a numeric array, respectively. Default OBJECT.

Usage

  1. if ( !function_exists( 'get_comment' ) ) { 
  2. require_once ABSPATH . WPINC . '/comment.php'; 
  3.  
  4. // Comment to retrieve. 
  5. $comment = &$comment = null; 
  6.  
  7. // Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to 
  8. // a WP_Comment object, an associative array, or a numeric array, respectively. Default OBJECT. 
  9. $output = ''; 
  10.  
  11. // NOTICE! Understand what this does before running. 
  12. $result = get_comment($comment, $output); 
  13.  

Defined (1)

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

/wp-includes/comment.php  
  1. function get_comment( &$comment = null, $output = OBJECT ) { 
  2. if ( empty( $comment ) && isset( $GLOBALS['comment'] ) ) { 
  3. $comment = $GLOBALS['comment']; 
  4.  
  5. if ( $comment instanceof WP_Comment ) { 
  6. $_comment = $comment; 
  7. } elseif ( is_object( $comment ) ) { 
  8. $_comment = new WP_Comment( $comment ); 
  9. } else { 
  10. $_comment = WP_Comment::get_instance( $comment ); 
  11.  
  12. if ( ! $_comment ) { 
  13. return null; 
  14.  
  15. /** 
  16. * Fires after a comment is retrieved. 
  17. * @since 2.3.0 
  18. * @param mixed $_comment Comment data. 
  19. */ 
  20. $_comment = apply_filters( 'get_comment', $_comment ); 
  21.  
  22. if ( $output == OBJECT ) { 
  23. return $_comment; 
  24. } elseif ( $output == ARRAY_A ) { 
  25. return $_comment->to_array(); 
  26. } elseif ( $output == ARRAY_N ) { 
  27. return array_values( $_comment->to_array() ); 
  28. return $_comment;