get_comment_author
云策文档标注
概述
get_comment_author 是一个 WordPress 过滤器钩子,用于修改返回的评论作者名称。它允许开发者自定义评论作者名的显示方式。
关键要点
- 钩子名称:get_comment_author
- 参数:$comment_author(评论作者的用户名)、$comment_id(评论ID,字符串类型)、$comment(WP_Comment 对象)
- 用途:过滤评论作者名,常用于自定义显示逻辑或数据修改
- 版本历史:从 WordPress 1.5.0 引入,4.1.0 版本添加了 $comment_id 和 $comment 参数
代码示例
add_filter('get_comment_author', 'custom_comment_author', 10, 3);
function custom_comment_author($comment_author, $comment_id, $comment) {
// 自定义逻辑,例如添加前缀或基于评论对象修改
return '作者: ' . $comment_author;
}注意事项
- 确保在函数中正确处理所有三个参数,以避免数据丢失或错误
- 此钩子与 get_comment_author() 函数关联,用于检索当前评论的作者
原文内容
Filters the returned comment author name.
Parameters
$comment_authorstring-
The comment author’s username.
$comment_idstring-
The comment ID as a numeric string.
$commentWP_Comment-
The comment object.
Source
return apply_filters( 'get_comment_author', $comment_author, $comment_id, $comment );