钩子文档

get_comment_author_link

💡 云策文档标注

概述

get_comment_author_link 是一个 WordPress 过滤器,用于修改评论作者链接的 HTML 输出。它允许开发者自定义链接的显示方式,例如链接到作者的文章归档页面。

关键要点

  • 过滤器名称:get_comment_author_link
  • 参数:$comment_author_link(HTML 格式的链接)、$comment_author(作者用户名)、$comment_id(评论 ID)
  • 依赖函数:get_comment_author_url() 和 get_comment_author() 依赖于 get_comment(),后者在 $comment_ID 为空时回退到全局 $comment 变量
  • 版本历史:4.1.0 版本添加了 $comment_author 和 $comment_id 参数,1.5.0 版本引入

代码示例

add_filter('get_comment_author_link', 'se_link_comment_to_archives');
function se_link_comment_to_archives( $link ) {
    global $comment;
    if ( !empty( $comment->user_id ) && !empty( get_userdata( $comment->user_id )->ID ) ) {
        $link = sprintf(
            '<a href="%s">%s</a>',
            get_author_posts_url( $comment->user_id ),
            strip_tags( $link )
        );
    }
    return $link;
}

注意事项

此代码示例仅适用于已验证并登录的用户提交的评论,确保 $comment->user_id 存在且有效。


📄 原文内容

Filters the comment author’s link for display.

Parameters

$comment_author_linkstring
The HTML-formatted comment author link.
Empty for an invalid URL.
$comment_authorstring
The comment author’s username.
$comment_idstring
The comment ID as a numeric string.

More Information

Both get_comment_author_url() and get_comment_author() rely on get_comment(), which falls back to the global comment variable if the $comment_ID argument is empty.

Source

return apply_filters( 'get_comment_author_link', $comment_author_link, $comment_author, $comment_id );

Changelog

Version Description
4.1.0 The $comment_author and $comment_id parameters were added.
1.5.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Link to author posts archive page:

    /**
      * To link to bbPress/BuddyPress profile or also link
      * avatar, see <a href="http://blog.samelh.com/?s=comment+author" rel="nofollow ugc">http://blog.samelh.com/?s=comment+author</a>
      */
    
    add_filter('get_comment_author_link', 'se_link_comment_to_archives');
    function se_link_comment_to_archives( $link ) {
        global $comment;
        if ( !empty( $comment->user_id ) && !empty( get_userdata( $comment->user_id )->ID ) ) {
       		$link = sprintf(
       			'<a href="%s" rel="external nofollow" class="url">%s</a>',
       			get_author_posts_url( $comment->user_id ),
       			strip_tags( $link )
       		);
        }
        return $link;
    }

    It should work as long as the comment was submitted by a verified and logged-in user on your blog.