WordPress at a glance

get_comment() WP 1.0

Retrieves comment data given a comment ID or comment object.

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.

  • Global. WP_Comment. $comment
Is the basis for: get_comment_text()
Works based on: WP_Comment()
✈ 1 time = 0.000436s = fast | 50000 times = 0.38s = very fast PHP 7.1.2RC1, WP 4.7.2
Hooks from function:
Return

WP_Comment/Array/null. Depends on $output value.

Usage

get_comment( &$comment, $output );
$comment(WP_Comment/string/int) (required)
Comment to retrieve.
$output(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

Code of get_comment: wp-includes/comment.php VER 4.9.8

<?php
function get_comment( &$comment = null, $output = OBJECT ) {
	if ( empty( $comment ) && isset( $GLOBALS['comment'] ) ) {
		$comment = $GLOBALS['comment'];
	}

	if ( $comment instanceof WP_Comment ) {
		$_comment = $comment;
	} elseif ( is_object( $comment ) ) {
		$_comment = new WP_Comment( $comment );
	} else {
		$_comment = WP_Comment::get_instance( $comment );
	}

	if ( ! $_comment ) {
		return null;
	}

	/**
	 * Fires after a comment is retrieved.
	 *
	 * @since 2.3.0
	 *
	 * @param mixed $_comment Comment data.
	 */
	$_comment = apply_filters( 'get_comment', $_comment );

	if ( $output == OBJECT ) {
		return $_comment;
	} elseif ( $output == ARRAY_A ) {
		return $_comment->to_array();
	} elseif ( $output == ARRAY_N ) {
		return array_values( $_comment->to_array() );
	}
	return $_comment;
}

Related Functions

From category: Comments

No comments
    Hello, !     Log In . Register