On this page
get_comment_excerpt( int|WP_Comment $comment_ID ): string
Retrieves the excerpt of the given comment.
Description
Returns a maximum of 20 words with an ellipsis appended if necessary.
Parameters
$comment_IDint|WP_Comment Required-
WP_Comment or ID of the comment for which to get the excerpt.
Default current comment.
Return
string The possibly truncated comment excerpt.
Source
File: wp-includes/comment-template.php. View all references
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 );
$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 $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', $excerpt, $comment->comment_ID, $comment );
}
Hooks
- apply_filters( 'comment_excerpt_length',
int $comment_excerpt_length ) -
Filters the maximum number of words used in the comment excerpt.
- apply_filters( 'get_comment_excerpt',
string $excerpt ,string $comment_ID ,WP_Comment $comment ) -
Filters the retrieved comment excerpt.
Related
Uses
| Uses | Description |
|---|---|
| wp_trim_words() wp-includes/formatting.php | Trims text to a certain number of words. |
| post_password_required() wp-includes/post-template.php | Determines whether the post requires password and whether a correct password has been provided. |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| _x() wp-includes/l10n.php | Retrieves translated string with gettext context. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| get_comment() wp-includes/comment.php | Retrieves comment data given a comment ID or comment object. |
Used By
| Used By | Description |
|---|---|
| comment_excerpt() wp-includes/comment-template.php | Displays the excerpt of the current comment. |
Changelog
| Version | Description |
|---|---|
| 4.4.0 | Added the ability for $comment_ID to also accept a WP_Comment object. |
| 1.5.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_comment_excerpt