get_comment_excerpt( int|WP_Comment $comment_id )

Retrieves the excerpt of the given comment.


Description

Returns a maximum of 20 words with an ellipsis appended if necessary.


Parameters

$comment_id

(Optional) WP_Comment or ID of the comment for which to get the excerpt.<br> Default current comment.


Return

(string) The possibly truncated comment excerpt.


Source

File: wp-includes/comment-template.php

function get_comment_excerpt( $comment_ID = 0 ) {
	$comment = get_comment( $comment_ID );
	$comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) );
	$words = explode( ' ', $comment_text );

	/**
	 * Filters the amount of words used in the comment excerpt.
	 *
	 * @since WP-4.4.0
	 *
	 * @param int $comment_excerpt_length The amount of words you want to display in the comment excerpt.
	 */
	$comment_excerpt_length = apply_filters( 'comment_excerpt_length', 20 );

	$use_ellipsis = count( $words ) > $comment_excerpt_length;
	if ( $use_ellipsis ) {
		$words = array_slice( $words, 0, $comment_excerpt_length );
	}

	$excerpt = trim( join( ' ', $words ) );
	if ( $use_ellipsis ) {
		$excerpt .= '&hellip;';
	}
	/**
	 * Filters the retrieved comment excerpt.
	 *
	 * @since WP-1.5.0
	 * @since WP-4.1.0 The `$comment_ID` and `$comment` parameters were added.
	 *
	 * @param string     $excerpt    The comment excerpt text.
	 * @param int        $comment_ID The comment ID.
	 * @param WP_Comment $comment    The comment object.
	 */
	return apply_filters( 'get_comment_excerpt', $excerpt, $comment->comment_ID, $comment );
}


Changelog

Changelog
Version Description
4.4.0 Added the ability for $comment_id to also accept a WP_Comment object.
1.5.0 Introduced.