函数文档

wp_new_comment_notify_postauthor()

💡 云策文档标注

概述

wp_new_comment_notify_postauthor() 函数用于向文章作者发送新评论通知。它基于评论类型和站点设置决定是否发送通知,并可通过过滤器覆盖。

关键要点

  • 函数接受一个必需的 $comment_id 参数,返回布尔值表示成功或失败。
  • 根据评论类型(普通评论或笔记)和 get_option() 获取的站点设置决定是否通知。
  • 使用 apply_filters('notify_post_author', $maybe_notify, $comment_id) 过滤器允许开发者覆盖通知行为。
  • 仅对已批准的评论或所有笔记发送通知,否则返回 false。
  • 内部调用 wp_notify_postauthor() 函数执行实际通知发送。

代码示例

// 示例:调用函数发送通知
$comment_id = 123;
$result = wp_new_comment_notify_postauthor($comment_id);
if ($result) {
    echo '通知发送成功';
} else {
    echo '通知发送失败';
}

注意事项

  • 函数自 WordPress 4.4.0 版本引入。
  • 通知行为受站点设置和过滤器影响,开发者可自定义逻辑。
  • 确保评论 ID 有效,否则可能返回 false。

📄 原文内容

Sends a notification of a new comment to the post author.

Parameters

$comment_idintrequired
Comment ID.

Return

bool True on success, false on failure.

Source

function wp_new_comment_notify_postauthor( $comment_id ) {
	$comment = get_comment( $comment_id );
	$is_note = ( $comment && 'note' === $comment->comment_type );

	$maybe_notify = $is_note ? get_option( 'wp_notes_notify', 1 ) : get_option( 'comments_notify' );

	/**
	 * Filters whether to send the post author new comment notification emails,
	 * overriding the site setting.
	 *
	 * @since 4.4.0
	 *
	 * @param bool $maybe_notify Whether to notify the post author about the new comment.
	 * @param int  $comment_id   The ID of the comment for the notification.
	 */
	$maybe_notify = apply_filters( 'notify_post_author', $maybe_notify, $comment_id );

	/*
	 * wp_notify_postauthor() checks if notifying the author of their own comment.
	 * By default, it won't, but filters can override this.
	 */
	if ( ! $maybe_notify ) {
		return false;
	}

	// Send notifications for approved comments and all notes.
	if (
		! isset( $comment->comment_approved ) ||
		( '1' !== $comment->comment_approved && ! $is_note ) ) {
			return false;
	}

	return wp_notify_postauthor( $comment_id );
}

Hooks

apply_filters( ‘notify_post_author’, bool $maybe_notify, int $comment_id )

Filters whether to send the post author new comment notification emails, overriding the site setting.

Changelog

Version Description
4.4.0 Introduced.