函数文档

get_comment_author_email()

💡 云策文档标注

概述

get_comment_author_email() 函数用于获取当前评论作者的电子邮件地址。它接受一个可选的评论 ID 或 WP_Comment 对象作为参数,并返回字符串类型的电子邮件地址。

关键要点

  • 函数返回当前评论作者的电子邮件地址,类型为字符串。
  • 参数 $comment_id 可选,可以是整数、WP_Comment 对象或默认当前评论。
  • 函数内部使用 get_comment() 获取评论数据,并通过 apply_filters() 应用 'get_comment_author_email' 钩子进行过滤。
  • 从 WordPress 4.4.0 版本起,$comment_id 参数支持 WP_Comment 对象。

代码示例

function get_comment_author_email( $comment_id = 0 ) {
    $comment = get_comment( $comment_id );
    return apply_filters( 'get_comment_author_email', $comment->comment_author_email, $comment->comment_ID, $comment );
}

注意事项

  • 确保在评论上下文中调用此函数,否则可能返回错误或空值。
  • 使用 'get_comment_author_email' 钩子可以自定义返回的电子邮件地址。

📄 原文内容

Retrieves the email of the author of the current comment.

Parameters

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

Return

string The current comment author’s email

Source

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

	/**
	 * Filters the comment author's returned email address.
	 *
	 * @since 1.5.0
	 * @since 4.1.0 The `$comment_id` and `$comment` parameters were added.
	 *
	 * @param string     $comment_author_email The comment author's email address.
	 * @param string     $comment_id           The comment ID as a numeric string.
	 * @param WP_Comment $comment              The comment object.
	 */
	return apply_filters( 'get_comment_author_email', $comment->comment_author_email, $comment->comment_ID, $comment );
}

Hooks

apply_filters( ‘get_comment_author_email’, string $comment_author_email, string $comment_id, WP_Comment $comment )

Filters the comment author’s returned email address.

Changelog

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