comment_text
云策文档标注
概述
comment_text 过滤器用于在显示评论文本时进行过滤,允许开发者修改评论内容。它接收评论文本、评论对象和参数数组,适用于数据库插入前和页面显示前的不同场景。
关键要点
- 过滤器名称:comment_text,用于过滤要显示的评论文本。
- 参数:$comment_text(评论文本字符串)、$comment(WP_Comment 对象或 null)、$args(参数数组)。
- 核心用途:在评论插入数据库前和页面显示前应用相同过滤规则,确保数据安全性和一致性。
- 注意事项:过滤器在核心代码中调用方式不同,$comment 参数可能为文本或对象,需在回调函数中处理可选性。
代码示例
function filter_text( $comment_text, $comment = null ) {
// 根据 $comment 是否存在执行不同操作
if ( null === $comment ) {
// 数据库插入前的处理
} else {
// 页面显示前的处理
}
return $comment_text;
}
add_filter( 'comment_text', 'filter_text', 10, 2 );注意事项
- 过滤器不适用于仪表板中的“最近评论”小部件,如需过滤该处内容,可使用 comment_excerpt 过滤器。
- 用户贡献笔记提供了实际应用示例,如移除 rel="nofollow" 属性,展示了过滤器的灵活用法。
原文内容
Filters the text of a comment to be displayed.
Description
See also
Parameters
$comment_textstring-
Text of the comment.
$commentWP_Comment|null-
The comment object. Null if not found.
$argsarray-
An array of arguments.
Source
echo apply_filters( 'comment_text', $comment_text, $comment, $args );
Changelog
| Version | Description |
|---|---|
| 1.2.0 | Introduced. |
Skip to note 4 content
gblsm
#24913 (comment_text filter used differently in two places in core) – WordPress Trac
In the first (
/wp-includes/comment.php), the$commentvariable contains just the comment text. In the second (/wp-includes/comment-template.php), the$commentvariable contains the actual comment object itself (based on the return ofget_comment( $comment_ID )).So the filters are filtering the same fields. The issue then becomes the number of parameters and that, too, is not a bug.
The first comment text filter is used specifically to filter the text of the comment before things like inserting it into the database. The second is used when filtering the text of the comment before displaying it on a page – this follows the pattern of not trusting data before inserting it in the DB and also not trusting the data when pulling it back out. Using the same filter here helps us use exactly the same rules to filter the content both pre and post DB.
If the problem is the number of fields, simply make the second parameter of your function optional:
function filter_text( $comment_text, $comment = null ) { // Do something to the comment, possibly switching based on the presence of $comment } add_filter( 'comment_text', 'filter_text', 10, 2 );If you only want to do things when the comment is inserted into the DB, just verify that
null === $commentin the function above. If you only want to do things when comments are displayed, verify thatnull !== $comment.IMO this isn’t a bug but a pretty powerful differentiating factor between a filter that can be used in different places to do either the same or different things.
Skip to note 5 content
WP SITES
Remove rel=”nofollow” from comment content text
add_filter( 'comment_text', 'remove_rel_nofollow_attribute' ); function remove_rel_nofollow_attribute( $comment_text ) { $comment_text = str_ireplace(' rel="nofollow"', '', $comment_text ); return $comment_text; }Skip to note 6 content
mgruson
Apparently this filter is not called in the “recent comments” widget of the activity (in the dashboard). Too bad (I’m trying to use this filter to obfuscate the content of selected comments, in case the user don’t have the rights to see the commented post).