Search code examples
ajaxwordpressvue.jssingle-page-applicationwordpress-rest-api

Email notification whenever a comment is created via WP REST API


I'm building a custom comment form for my Vue.js & WordPress Single Page Application theme and was able to post a comment via ajax POST request to the WP REST API. But I don't get any admin notifications on a new comment even the settings in Settings-->Reading are set to notify the admin each time a comment is created/added.

So how can I get email notifications on WP REST API comment creation?


Solution

  • For any reason the WP REST API team didn't use the function wp_new_comment whenever a comment is added/created. This function includes the comment_post action hook which, in turn, is used by WordPress to send admin notifications in wp-includes/default-filters.php.

    Instead they've used the wp_insert_comment() function which is defined in wp-includes/comments.php and which also includes an action hook of the same name wp_insert_comment at the very end of the function. This hook we can use to trigger the notification function wp_new_comment_notify_moderator(). Just add the following snippet into your theme's / plugin's functions.php

    add_action( 'wp_insert_comment', 'wp_new_comment_notify_moderator' );
    

    see also:

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

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