函数文档

get_comment_text()

💡 云策文档标注

概述

get_comment_text() 函数用于获取指定评论的文本内容,支持通过参数和过滤器进行自定义处理。它默认返回当前评论的内容,并在评论源中为子评论添加回复前缀。

关键要点

  • 函数签名:get_comment_text( $comment_id = 0, $args = array() ),返回评论内容的字符串。
  • 参数 $comment_id 可选,可以是评论 ID 或 WP_Comment 对象,默认为当前评论。
  • 参数 $args 可选,是一个数组,用于传递额外参数,如格式、回调函数等。
  • 在评论源中,如果评论有父评论,会自动添加“In reply to %s.”前缀。
  • 可通过 apply_filters( 'get_comment_text', $comment_text, $comment, $args ) 过滤器修改评论文本。
  • 相关函数包括 get_comment()、is_comment_feed()、get_comment_link() 等。
  • 版本变更:5.4.0 添加了评论源中子评论的前缀,4.4.0 支持 $comment_id 接受 WP_Comment 对象。

代码示例

// 示例:使用过滤器修改特定评论的文本
add_filter( 'get_comment_text', 'wpdocs_comment', 10, 2 );
function wpdocs_comment( $text_content, WP_Comment $com ) {
    if ( ! is_admin() && in_array( $com->comment_ID, array( 723, 15 ) ) ) {
        $text_content = __( 'You've Just Been Erased!' );
    }
    return $text_content;
}

注意事项

  • $args 参数的具体效果可能因 WordPress 版本而异,例如 'max_depth' 在 WP 6.2 中可能无效。
  • 过滤器 get_comment_text 允许在输出前动态修改评论内容,适用于自定义格式化或条件处理。
  • 函数内部使用 ent2ncr() 处理实体转换,确保在评论源中的正确显示。

📄 原文内容

Retrieves the text of the current comment.

Description

See also

Parameters

$comment_idint|WP_Commentoptional
WP_Comment or ID of the comment for which to get the text.
Default current comment.
$argsarrayoptional
An array of arguments.

Default:array()

Return

string The comment content.

Source

function get_comment_text( $comment_id = 0, $args = array() ) {
	$comment = get_comment( $comment_id );

	$comment_text = $comment->comment_content;

	if ( is_comment_feed() && $comment->comment_parent ) {
		$parent = get_comment( $comment->comment_parent );
		if ( $parent ) {
			$parent_link = esc_url( get_comment_link( $parent ) );
			$name        = get_comment_author( $parent );

			$comment_text = sprintf(
				/* translators: %s: Comment link. */
				ent2ncr( __( 'In reply to %s.' ) ),
				'<a href="' . $parent_link . '">' . $name . '</a>'
			) . "nn" . $comment_text;
		}
	}

	/**
	 * Filters the text of a comment.
	 *
	 * @since 1.5.0
	 *
	 * @see Walker_Comment::comment()
	 *
	 * @param string     $comment_text Text of the comment.
	 * @param WP_Comment $comment      The comment object.
	 * @param array      $args         An array of arguments.
	 */
	return apply_filters( 'get_comment_text', $comment_text, $comment, $args );
}

Hooks

apply_filters( ‘get_comment_text’, string $comment_text, WP_Comment $comment, array $args )

Filters the text of a comment.

Changelog

Version Description
5.4.0 Added ‘In reply to %s.’ prefix to child comments in comments feed.
4.4.0 Added the ability for $comment_id to also accept a WP_Comment object.
1.5.0 Introduced.

User Contributed Notes

  1. Skip to note 4 content

    Change comment texts of specific comments

    /**
     * For comments with ID 723 or 15
     */
    add_filter( 'get_comment_text', 'wpdocs_comment', 10, 2 );
    
    function wpdocs_comment( $text_content, WP_Comment $com ) {
        if ( ! is_admin() && in_array( $com->comment_ID, array( 723, 15 ) ) ) {
            $text_content = __( 'You've Just Been Erased!' );
        }
    
        return $text_content;
    }

  2. Skip to note 5 content

    Here are the accepted arguments (not verified/tested) of the $args parameter for the get_comment_text() function:

    format‘ – Specifies the format of the comment text. The accepted values are ‘html’ (default) or ‘plain’.

    short_ping‘ – Specifies whether to display the comment text as a “pingback” or “trackback” instead of a regular comment. The accepted values are true or false.

    avatar_size‘ – Specifies the size of the avatar image displayed with the comment text. The accepted values are integers representing the avatar size in pixels.

    callback‘ – Specifies a callback function to use to display the comment text. This allows for custom formatting of the comment text.

    end-callback‘ – Specifies a callback function to use to display the closing HTML tags after the comment text. This is used in conjunction with the ‘callback’ argument.

    type‘ – Specifies the type of comment to display. The accepted values are ‘comment’, ‘pingback’, or ‘trackback’.

    reply_text‘ – Specifies the text to use for the “Reply” link for threaded comments. The default value is ‘Reply’.

    max_depth‘ – Specifies the maximum depth of threaded comments to display. The default value is 0, which means all levels of threaded comments will be displayed.

    echo‘ – Specifies whether to output the comment text immediately (true) or return it as a string (false). The default value is true.

    Note that not all arguments are accepted by all WordPress versions.

    P.S. Tested ‘max_depth’ in WP 6.2, not worked.

    $args = array( 'max_depth' => 1 ); // limit comments to one level deep
    $parent_comment = get_comment_text( $comment->comment_parent, $args );