函数文档

get_comment_author_url()

💡 云策文档标注

概述

get_comment_author_url() 函数用于获取当前评论作者的 URL,返回纯文本字符串而非链接。它接受一个可选的评论 ID 或 WP_Comment 对象作为参数,并应用过滤器以允许自定义输出。

关键要点

  • 函数返回评论作者的 URL,如果未提供则返回空字符串。
  • 参数 $comment_id 可选,可以是评论 ID 或 WP_Comment 对象,默认为当前评论。
  • 内部使用 get_comment() 获取评论数据,并通过 esc_url() 清理 URL。
  • 应用了 get_comment_author_url 过滤器,允许开发者修改返回的 URL。
  • 与 comment_author_url() 函数不同,后者直接显示 URL,而此函数仅返回字符串。

代码示例

$comment_author = get_comment_author_url();

注意事项

  • URL 会经过 esc_url() 清理,仅允许 http 和 https 协议。
  • 如果评论作者的 URL 为 'http://',函数会将其视为空字符串处理。
  • 从 WordPress 4.4.0 开始,$comment_id 参数支持 WP_Comment 对象。

📄 原文内容

Retrieves the URL of the author of the current comment, not linked.

Parameters

$comment_idint|WP_Commentoptional
WP_Comment or the ID of the comment for which to get the author’s URL.
Default current comment.

Return

string Comment author URL, if provided, an empty string otherwise.

Source

function get_comment_author_url( $comment_id = 0 ) {
	$comment = get_comment( $comment_id );

	$comment_author_url = '';
	$comment_id         = 0;

	if ( ! empty( $comment ) ) {
		$comment_author_url = ( 'http://' === $comment->comment_author_url ) ? '' : $comment->comment_author_url;
		$comment_author_url = esc_url( $comment_author_url, array( 'http', 'https' ) );

		$comment_id = $comment->comment_ID;
	}

	/**
	 * Filters the comment author's URL.
	 *
	 * @since 1.5.0
	 * @since 4.1.0 The `$comment_id` and `$comment` parameters were added.
	 *
	 * @param string          $comment_author_url The comment author's URL, or an empty string.
	 * @param string|int      $comment_id         The comment ID as a numeric string, or 0 if not found.
	 * @param WP_Comment|null $comment            The comment object, or null if not found.
	 */
	return apply_filters( 'get_comment_author_url', $comment_author_url, $comment_id, $comment );
}

Hooks

apply_filters( ‘get_comment_author_url’, string $comment_author_url, string|int $comment_id, WP_Comment|null $comment )

Filters the comment author’s URL.

Changelog

Version Description
4.4.0 Added the ability for $comment_id to also accept a WP_Comment object.
1.5.0 Introduced.

User Contributed Notes