get_comment_excerpt()WP 1.5.0

Gets a trimmed version of the current or specified comment (maximum 20 words).

If the text was trimmed, will be added at the end.

The output is not processed by the function wpautop().

1 time — 0.000609 sec (slow) | 50000 times — 5 sec (fast)

Returns

String. Trimmed/untrimmed text.

Usage

get_comment_excerpt( $comment_ID );
$comment_ID(int/WP_Comment) (required)
ID or comment object to check for length, and if necessary, trim.
Default: 0 (current comment in the loop)

Examples

0

#1 Cut the text of the comment to 20 words, if it is more.

echo get_comment_excerpt( 1024 );

// output:
// Looked at Get Recent Comments - messed up a mountain of code :)  Tested my code queries creates a standard function of WordPress..

Changelog

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

get_comment_excerpt() code WP 6.9

function get_comment_excerpt( $comment_id = 0 ) {
	$comment = get_comment( $comment_id );

	if ( ! post_password_required( $comment->comment_post_ID ) ) {
		$comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) );
	} else {
		$comment_text = __( 'Password protected' );
	}

	/* translators: Maximum number of words used in a comment excerpt. */
	$comment_excerpt_length = (int) _x( '20', 'comment_excerpt_length' );

	/**
	 * Filters the maximum number of words used in the comment excerpt.
	 *
	 * @since 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', $comment_excerpt_length );

	$comment_excerpt = wp_trim_words( $comment_text, $comment_excerpt_length, '…' );

	/**
	 * Filters the retrieved comment excerpt.
	 *
	 * @since 1.5.0
	 * @since 4.1.0 The `$comment_id` and `$comment` parameters were added.
	 *
	 * @param string     $comment_excerpt The comment excerpt text.
	 * @param string     $comment_id      The comment ID as a numeric string.
	 * @param WP_Comment $comment         The comment object.
	 */
	return apply_filters( 'get_comment_excerpt', $comment_excerpt, $comment->comment_ID, $comment );
}