函数文档

comment_author_email()

💡 云策文档标注

概述

comment_author_email() 函数用于显示当前全局 $comment 中评论作者的电子邮件地址。开发者需注意保护电子邮件地址,防止被爬虫抓取,并确保不直接以原始形式显示在网站上。

关键要点

  • 函数功能:输出评论作者的电子邮件地址,基于当前评论或指定评论。
  • 参数:$comment_id(可选),可以是评论 ID 或 WP_Comment 对象,默认使用当前评论。
  • 安全注意事项:应采取措施保护电子邮件地址,避免被恶意爬虫获取,确保不直接暴露原始地址。
  • 相关函数:与 get_comment_author_email()、get_comment() 和 apply_filters() 紧密相关。
  • Hook:使用 apply_filters('author_email', $comment_author_email, $comment_id) 过滤显示的电子邮件地址。
  • 版本变更:从 4.4.0 版本起,$comment_id 参数支持 WP_Comment 对象。

代码示例

function comment_author_email( $comment_id = 0 ) {
    $comment = get_comment( $comment_id );
    $comment_author_email = get_comment_author_email( $comment );
    /**
     * Filters the comment author's email for display.
     *
     * @since 1.2.0
     * @since 4.1.0 The `$comment_id` parameter was added.
     *
     * @param string $comment_author_email The comment author's email address.
     * @param string $comment_id           The comment ID as a numeric string.
     */
    echo apply_filters( 'author_email', $comment_author_email, $comment->comment_ID );
}

注意事项

在输出电子邮件地址时,务必考虑隐私和安全问题,避免直接显示原始地址,以防止被滥用或爬虫抓取。建议使用过滤或加密措施。


📄 原文内容

Displays the email of the author of the current global $comment.

Description

Care should be taken to protect the email address and assure that email harvesters do not capture your commenter’s email address. Most assume that their email address will not appear in raw form on the site. Doing so will enable anyone, including those that people don’t want to get the email address and use it for their own means good and bad.

Parameters

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

Source

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

	$comment_author_email = get_comment_author_email( $comment );

	/**
	 * Filters the comment author's email for display.
	 *
	 * @since 1.2.0
	 * @since 4.1.0 The `$comment_id` parameter was added.
	 *
	 * @param string $comment_author_email The comment author's email address.
	 * @param string $comment_id           The comment ID as a numeric string.
	 */
	echo apply_filters( 'author_email', $comment_author_email, $comment->comment_ID );
}

Hooks

apply_filters( ‘author_email’, string $comment_author_email, string $comment_id )

Filters the comment author’s email for display.

Changelog

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

User Contributed Notes