每当通过 WP REST 创建评论时发送电子邮件通知 API

Email notification whenever a comment is created via WP REST API

我正在为我的 Vue.js & WordPress Single Page Application theme 构建一个自定义评论表单,并且能够通过 ajax POST 向 WP REST 请求 post 评论 API.但是我没有收到关于新评论的任何管理员通知,即使设置中的设置-->阅读设置为每次评论 created/added 时通知管理员。

那么如何在 WP REST API 创建评论时收到电子邮件通知?

出于任何原因,WP REST API 团队没有在评论 added/created 时使用函数 wp_new_comment。此函数包括 comment_post 操作挂钩,WordPress 使用该挂钩在 wp-includes/default-filters.php 中发送管理员通知。

相反,他们使用了 wp_insert_comment() 函数,该函数在 wp-includes/comments.php 中定义,并且在函数的最后还包含一个同名的动作挂钩 wp_insert_comment。这个钩子我们可以用来触发通知函数wp_new_comment_notify_moderator()。只需将以下代码片段添加到您的主题/插件的 functions.php

add_action( 'wp_insert_comment', 'wp_new_comment_notify_moderator' );

另见:

https://core.trac.wordpress.org/ticket/40352

https://wordpress.org/support/topic/wp-api-comments-not-sending-notifications/#post-8987973