Publish: 2014-11-19 | Modify: 2020-03-29
Recently, my blog has had some issues with loading slowly due to the Gravatar avatars being blocked and the instability of the server. Therefore, I switched back to using the default WordPress comments for simplicity and improved speed. However, in order to enhance the user experience, I wanted to implement a comment reply email notification feature.
There are many methods available online, but some of them are no longer functional and others are incomplete. Fortunately, a helpful user named "无主题" provided a good piece of code that can be added to the functions.php file in the theme directory.
// 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 [' . 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;">></span>You have a new reply on <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) . ',</p>
<p>You previously commented on the article "<em>' . get_the_title($comment->comment_post_ID) . '</em>":</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>Here is the reply from ' . trim($comment->comment_author) . ':</p>
<p style="background-color: #f5f5f5;border: 0px solid #DDD;padding: 10px 15px;margin:18px 0">' . trim($comment->comment_content) .'</p>
<p>You can <a href="' . htmlspecialchars(get_comment_link($parent_id, array('type' => 'comment'))) . '">click here</a> to view the complete reply. Welcome back to <a href="' . get_option('home') . '">' . get_option('blogname') . '</a>!</p>
<p style="color: #000;background: #f5f5f5;font-size:11px;border: solid 1px #eee;padding: 2px 10px;">Note: This email was automatically sent from <a href="' . get_option('home') . '">' . get_option('blogname') . '</a>. Please do not reply directly.<br />If you did not request this email, 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 ----------------------------------------
Please refer to the screenshot below for the display effect. If the mail() function is disabled on your hosting, I recommend using the WP SMTP plugin or implementing SMTP code to resolve the issue.
Email reply notification
I come from China and I am a freelancer. I specialize in Linux operations, PHP, Golang, and front-end development. I have developed open-source projects such as Zdir, ImgURL, CCAA, and OneNav.