How to Enable Email Notifications for WordPress Comment Replies

wordpress comment reply email notificationwordpress functions.php codewp smtp pluginwordpress email notificationcomment reply notification
Published·Modified·

Originally, Xiaoz's blog used Duoshuo for comments, where replying to a comment would trigger a notification from Duoshuo. However, recently Gravatar avatars have been blocked in China, and the Duoshuo server has become unstable. Some avatars fail to cache properly, causing the blog to load extremely slowly. See here for a solution on enabling Gravatar caching to fix slow WordPress loading.

Therefore, I recently switched back to WordPress's built-in comment system to simplify things and improve speed. However, to enhance user experience, I wanted to ensure that when someone leaves a comment and I reply to them, they receive a notification. This can be achieved by enabling the comment reply email notification feature.

There are many code snippets available online for this purpose. I tried several, but some are no longer functional, while others are incomplete. Finally, a helpful netizen from Wuzhuti's blog provided a reliable code snippet. You only need to add this code to your theme's functions.php file.

// Comment reply email notification
function comment_mail_notify($comment_id) {
  $admin_email = get_bloginfo ('admin_email'); 
  $comment = get_comment($comment_id);
  $comment_author_email = trim($comment->comment_author_email);
  $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
  $to = $parent_id ? trim(get_comment($parent_id)->comment_author_email) : '';
  $spam_confirmed = $comment->comment_approved;
  if (($parent_id != '') && ($spam_confirmed != 'spam') && ($to != $admin_email)) {
	$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
	$subject = 'You have a new reply on your comment in [' . get_option("blogname") . ']';
	$message = '
		<div style="-moz-border-radius: 5px;-webkit-border-radius: 5px;-khtml-border-radius: 5px;border-radius: 5px;background-color:white;border-top:2px solid #12ADDB;box-shadow:0 1px 3px #AAAAAA;line-height:180%;padding:0 15px 12px;width:500px;margin:50px auto;color:#555555;font-family:Century Gothic,Trebuchet MS,Hiragino Sans GB,微软雅黑,Microsoft Yahei,Tahoma,Helvetica,Arial,SimSun,sans-serif;font-size:12px;">
		<h2 style="border-bottom:1px solid #DDD;font-size:14px;font-weight:normal;padding:13px 0 10px 8px;"><span style="color: #12ADDB;font-weight: bold;">&gt;</span> You have a new reply on your comment in <a style="text-decoration:none;color: #12ADDB;" href="' . get_option('home') . '" target="_blank" rel="noopener noreferrer">' . get_option('blogname') . '</a> blog!</h2><div style="padding:0 12px 0 12px;margin-top:18px"><p>Dear ' . trim(get_comment($parent_id)->comment_author) . ', Hello! You commented on the article titled "' . get_the_title($comment->comment_post_ID) . '":</p>
		<p style="background-color: #f5f5f5;border: 0px solid #DDD;padding: 10px 15px;margin:18px 0">' . trim(get_comment($parent_id)->comment_content) . '</p><p>' . trim($comment->comment_author) . ' replied to you as follows:</p><p style="background-color: #f5f5f5;border: 0px solid #DDD;padding: 10px 15px;margin:18px 0">' . trim($comment->comment_content) .'</p><p>You can click <a href="' . htmlspecialchars(get_comment_link($parent_id, array('type' => 'comment'))) . '">to view the full reply content</a>, and welcome to visit <a href="' . get_option('home') . '">' . get_option('blogname') . '</a> again.</p>
		<p style="color: #000;background: #f5f5f5;font-size:11px;border: solid 1px #eee;padding: 2px 10px;">Note: This email is automatically sent by <a href="' . get_option('home') . '">' . get_option('blogname') . '</a>, please do not reply directly.<br />If this email was not requested by you, please ignore and delete it!</p></div></div>';
	$from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
	$headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
	wp_mail( $to, $subject, $message, $headers );
  }
}
add_action('comment_post', 'comment_mail_notify');
// -- END ----------------------------------------

You can see the result in the screenshot below. If your hosting provider has disabled the mail() function, it is recommended to use the WP SMTP plugin or configure SMTP code to resolve the issue.

huifu
Email Reply Notification