get_comment_author_url_link()
云策文档标注
概述
get_comment_author_url_link() 函数用于获取当前评论作者 URL 的 HTML 链接,支持自定义链接文本和前后包装内容。
关键要点
- 函数返回当前评论作者 URL 的 HTML 链接,格式为 $before + 链接 + $after。
- 参数 $link_text 仅在评论作者无 URL 时使用,否则忽略;$before 和 $after 用于包装链接。
- 函数内部处理 URL 显示文本,移除 'http://www.' 和 'http://' 前缀,并修剪尾部斜杠。
- 可通过 'get_comment_author_url_link' 过滤器钩子修改返回的链接。
- 新增 $comment 参数(自 4.6.0 版本起),支持指定评论 ID 或 WP_Comment 对象。
代码示例
function get_comment_author_url_link( $link_text = '', $before = '', $after = '', $comment = 0 ) {
$comment_author_url = get_comment_author_url( $comment );
$display = ( '' !== $link_text ) ? $link_text : $comment_author_url;
$display = str_replace( 'http://www.', '', $display );
$display = str_replace( 'http://', '', $display );
if ( str_ends_with( $display, '/' ) ) {
$display = substr( $display, 0, -1 );
}
$comment_author_url_link = $before . sprintf(
'%2$s',
$comment_author_url,
$display
) . $after;
/**
* Filters the comment author's returned URL link.
*
* @since 1.5.0
*
* @param string $comment_author_url_link The HTML-formatted comment author URL link.
*/
return apply_filters( 'get_comment_author_url_link', $comment_author_url_link );
}注意事项
- 确保在评论循环中调用此函数以获取正确的当前评论。
- 使用过滤器时,注意参数类型为字符串,避免破坏 HTML 结构。
原文内容
Retrieves the HTML link of the URL of the author of the current comment.
Description
$link_text parameter is only used if the URL does not exist for the comment author. If the URL does exist then the URL will be used and the $link_text will be ignored.
Encapsulate the HTML link between the $before and $after. So it will appear in the order of $before, link, and finally $after.
Parameters
$link_textstringoptional-
The text to display instead of the comment author’s email address. Default empty.
$beforestringoptional-
The text or HTML to display before the email link.
Default empty. $afterstringoptional-
The text or HTML to display after the email link.
Default empty. $commentint|WP_Commentoptional-
Comment ID or WP_Comment object.
Default is the current comment.
Source
function get_comment_author_url_link( $link_text = '', $before = '', $after = '', $comment = 0 ) {
$comment_author_url = get_comment_author_url( $comment );
$display = ( '' !== $link_text ) ? $link_text : $comment_author_url;
$display = str_replace( 'http://www.', '', $display );
$display = str_replace( 'http://', '', $display );
if ( str_ends_with( $display, '/' ) ) {
$display = substr( $display, 0, -1 );
}
$comment_author_url_link = $before . sprintf(
'<a href="%1$s" rel="external">%2$s</a>',
$comment_author_url,
$display
) . $after;
/**
* Filters the comment author's returned URL link.
*
* @since 1.5.0
*
* @param string $comment_author_url_link The HTML-formatted comment author URL link.
*/
return apply_filters( 'get_comment_author_url_link', $comment_author_url_link );
}
Hooks
- apply_filters( ‘get_comment_author_url_link’, string $comment_author_url_link )
-
Filters the comment author’s returned URL link.