Skip to main content

User Email Comment Notifications in WordPress

How to add comment notifications for users and update the moderation notice without a plugin (using the functions.php file).
November 2, 2023

Image credit: Midjourney

I’ve been using the “Subscribe To Comments Reloaded” for a few years. Although the core features worked fine, some items like email security (especially Google reCAPTCHA) seemed to stop working.

After a lot of research and testing, I came up with an easier solution that is super lightweight and doesn’t require a plugin. You’ll just update your child theme’sfunctions.php” file.

The code below does four things:

  1. Sets up the emails to be sent in HTML.
  2. Emails a commenter when their comment is approved.
  3. Emails a commenter when someone replies to their comment.
  4. Allows you to customize the “Your comment is awaiting moderation” text as well as adds an “awaiting-moderation” class to this text to help you stylize this alert.
// =========================================================================
//  Comment Notifications &  Moderation Notice
// =========================================================================

// Set HTML mail content type
function set_html_content_type() {
  return 'text/html';
}

// Notify commenter when comment is approved
add_action('wp_set_comment_status', 'notify_comment_author_on_approval', 10, 2);
function notify_comment_author_on_approval($comment_id, $comment_status) {
  if ($comment_status == 'approve') {
    $comment = get_comment($comment_id);
    $email = $comment->comment_author_email;
    $post = get_post($comment->comment_post_ID);
    $subject = 'Your comment on ' . $post->post_title . ' was approved';
    $message = 'Hi ' . $comment->comment_author . ',<br><br>';
    $message .= 'Your comment on "' . $post->post_title . '" has been approved!<br><br>';
    $message .= '<a href="' . get_comment_link($comment) . '">View your comment here</a>.<br><br>';
    $message .= 'Thanks,<br>';
    $message .= 'YOUR NAME<br>';
    $message .= get_bloginfo('name');

    add_filter('wp_mail_content_type', 'set_html_content_type');
    wp_mail($email, $subject, $message);
    remove_filter('wp_mail_content_type', 'set_html_content_type');
  }
}

// Notify commenter when comment is replied to
add_action('comment_post', 'notify_comment_author_on_reply', 10, 2);
function notify_comment_author_on_reply($comment_id, $comment_approved) {
  $comment = get_comment($comment_id);
  if ($comment->comment_parent) {
    $parent_comment = get_comment($comment->comment_parent);
    $email = $parent_comment->comment_author_email;
    $post = get_post($comment->comment_post_ID);
    $subject = 'Someone replied to your comment on ' . $post->post_title;
    $message = 'Hi ' . $parent_comment->comment_author . ',<br><br>';
    $message .= 'Someone has replied to your comment on "' . $post->post_title . '"!<br><br>';
    $message .= '<a href="' . get_comment_link($comment) . '">View your reply here</a>.<br><br>';
    $message .= 'Thanks,<br>';
    $message .= 'YOUR NAME<br>';
    $message .= get_bloginfo('name');

    add_filter('wp_mail_content_type', 'set_html_content_type');
    wp_mail($email, $subject, $message);
    remove_filter('wp_mail_content_type', 'set_html_content_type');
  }
}

// Customize 'Your comment is awaiting moderation.' text
add_filter( 'gettext', function( $text ) {
  if ( 'Your comment is awaiting moderation.' === $text ) {
    $text = '<span class="awaiting-moderation">Thanks! you\'ll be notified you once your comment has been approved.</span>';
  }
  return $text;
});

Caveats

  • Change the “YOUR NAME” in the code above to your name, or remove this line completely.
  • This doesn’t allow users to unsubscribe or manage email subscriptions to your posts.
  • Make sure you’re doing any updates to “functions.php” in a child theme.
  • You’ll need to make sure that email is processing properly.

Recommendations on preventing spam:

Did you like this content?
Please consider subscribing to be notified when something new is added.


What do you think?

Your email address will not be published. Required fields are marked *