get_comment_author_link()
云策文档标注
概述
get_comment_author_link() 函数用于获取当前评论作者的 HTML 链接。它基于评论 ID 或 WP_Comment 对象,返回作者名称或带链接的 HTML 字符串,并包含 rel 属性处理。
关键要点
- 函数返回评论作者的 HTML 链接,若作者 URL 为空或为 'http://',则仅返回作者名称。
- 参数 $comment_id 可选,可为整数或 WP_Comment 对象,默认为当前评论。
- 内部使用 get_comment_author_url() 和 get_comment_author() 获取数据,并依赖 get_comment() 函数。
- 包含两个过滤器:comment_author_link_rel 用于修改 rel 属性,get_comment_author_link 用于修改最终输出链接。
- 支持外部链接时自动添加 'external' 和 'nofollow' 到 rel 属性。
代码示例
echo get_comment_author_link( $comment_ID );注意事项
- 函数在 WordPress 4.4.0 版本后支持 $comment_id 参数接受 WP_Comment 对象。
- 可通过过滤器自定义输出,例如添加 CSS 类到链接中。
原文内容
Retrieves the HTML link to the URL of the author of the current comment.
Description
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.
Parameters
$comment_idint|WP_Commentoptional-
WP_Comment or the ID of the comment for which to get the author’s link.
Default current comment.
Source
function get_comment_author_link( $comment_id = 0 ) {
$comment = get_comment( $comment_id );
if ( ! empty( $comment->comment_ID ) ) {
$comment_id = $comment->comment_ID;
} elseif ( is_scalar( $comment_id ) ) {
$comment_id = (string) $comment_id;
} else {
$comment_id = '0';
}
$comment_author_url = get_comment_author_url( $comment );
$comment_author = get_comment_author( $comment );
if ( empty( $comment_author_url ) || 'http://' === $comment_author_url ) {
$comment_author_link = $comment_author;
} else {
$rel_parts = array( 'ugc' );
if ( ! wp_is_internal_link( $comment_author_url ) ) {
$rel_parts = array_merge(
$rel_parts,
array( 'external', 'nofollow' )
);
}
/**
* Filters the rel attributes of the comment author's link.
*
* @since 6.2.0
*
* @param string[] $rel_parts An array of strings representing the rel tags
* which will be joined into the anchor's rel attribute.
* @param WP_Comment $comment The comment object.
*/
$rel_parts = apply_filters( 'comment_author_link_rel', $rel_parts, $comment );
$rel = implode( ' ', $rel_parts );
$rel = esc_attr( $rel );
// Empty space before 'rel' is necessary for later sprintf().
$rel = ! empty( $rel ) ? sprintf( ' rel="%s"', $rel ) : '';
$comment_author_link = sprintf(
'<a href="%1$s" class="url"%2$s>%3$s</a>',
$comment_author_url,
$rel,
$comment_author
);
}
/**
* Filters the comment author's link for display.
*
* @since 1.5.0
* @since 4.1.0 The `$comment_author` and `$comment_id` parameters were added.
*
* @param string $comment_author_link The HTML-formatted comment author link.
* Empty for an invalid URL.
* @param string $comment_author The comment author's username.
* @param string $comment_id The comment ID as a numeric string.
*/
return apply_filters( 'get_comment_author_link', $comment_author_link, $comment_author, $comment_id );
}
Hooks
- apply_filters( ‘comment_author_link_rel’, string[] $rel_parts, WP_Comment $comment )
-
Filters the rel attributes of the comment author’s link.
- apply_filters( ‘get_comment_author_link’, string $comment_author_link, string $comment_author, string $comment_id )
-
Filters the comment author’s link for display.
Changelog
| Version | Description |
|---|---|
| 4.4.0 | Added the ability for $comment_id to also accept a WP_Comment object. |
| 1.5.0 | Introduced. |
Skip to note 2 content
Muhammad Ayoub
Add an custom class to comment_author_link using
filters. When usingget_comment_author_linkthis function to fetch comment author link.Just add this code into functions.php
// filter for get_comment_author_link function wpdocs_custom_get_comment_author_link( $content ) { $extra_classes = 'text-decoration-none text-primary fw-bold mb-0'; return preg_replace( '/url/', 'url ' . $extra_classes, $content ); } add_filter( 'get_comment_author_link', 'wpdocs_custom_get_comment_author_link', 99 );