Code Implementation Method
There are two methods to implement the feature of sending emails to commenters after a blog comment on WordPress: one is by adding code, and the other is by using a plugin.If using the code method, the following code (or a slightly modified version) is commonly used: /* comment_mail_notify v1.0 by willin kan. (All replies send emails) */ Log in to your blog„s Admin Dashboard, click the “Edit„ option under the “Appearance„ tab to enter the Theme editing interface, and add the following function between the tags in the functions.php file:1. Send emails to everyone/* comment_mail_notify v1.0 by willin kan. (所有回复都发邮件) */
function comment_mail_notify($comment_id) {
$comment = get_comment($comment_id);
$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
$spam_confirmed = $comment->comment_approved;
if (($parent_id != '') && ($spam_confirmed != 'spam')) {
$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); //e-mail 发出点, no-reply 可改为可用的 e-mail.
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = '您在 [' . get_option("blogname") . '] 的留言有了回复';
$message = '
' . trim(get_comment($parent_id)->comment_author) . ', 您好!
您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:
'
. trim(get_comment($parent_id)->comment_content) . '
' . trim($comment->comment_author) . ' 给您的回复:
'
. trim($comment->comment_content) . '
您可以点击 查看回复完整內容
欢迎再度光临 ' . get_option('blogname') . '
(此邮件由系统自动发送,请勿回复.)
';
$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 );
//echo 'mail to ', $to, '
' , $subject, $message; // for testing
}
}
add_action('comment_post', 'comment_mail_notify');
// -- END ----------------------------------------2. Let visitors choose whether to receive email notificationsAdd the following function between the tags in the functions.php file. This function will generate an option at the bottom of the comment box asking whether to receive reply notifications./* 开始*/
function comment_mail_notify($comment_id) {
$admin_notify = '1'; // admin 要不要收回复通知 ( '1'=要 ; '0'=不要 )
$admin_email = get_bloginfo ('admin_email'); // $admin_email 可改为你指定的 e-mail.
$comment = get_comment($comment_id);
$comment_author_email = trim($comment->comment_author_email);
$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
global $wpdb;
if ($wpdb->query("Describe {$wpdb->comments} comment_mail_notify") == '')
$wpdb->query("ALTER TABLE {$wpdb->comments} ADD COLUMN comment_mail_notify TINYINT NOT NULL DEFAULT 0;");
if (($comment_author_email != $admin_email && isset($_POST['comment_mail_notify'])) || ($comment_author_email == $admin_email && $admin_notify == '1'))
$wpdb->query("UPDATE {$wpdb->comments} SET comment_mail_notify='1' WHERE comment_ID='$comment_id'");
$notify = $parent_id ? get_comment($parent_id)->comment_mail_notify : '0';
$spam_confirmed = $comment->comment_approved;
if ($parent_id != '' && $spam_confirmed != 'spam' && $notify == '1') {
$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 发出点, no-reply 可改为可用的 e-mail.
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = '您在 [' . get_option("blogname") . '] 的留言有了回复';
$message = '
' . trim(get_comment($parent_id)->comment_author) . ', 您好!
您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:
'
. trim(get_comment($parent_id)->comment_content) . '
' . trim($comment->comment_author) . ' 给您的回复:
'
. trim($comment->comment_content) . '
您可以点击查看回复的完整內容
还要再度光临 ' . get_option('blogname') . '
(此邮件由系统自动发送,请勿回复.)
';
$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 );
//echo 'mail to ', $to, '
' , $subject, $message; // for testing
}
}
add_action('comment_post', 'comment_mail_notify');
/* 自动加勾选栏 */
function add_checkbox() {
echo '有人回复时邮件通知我';
}
add_action('comment_form', 'add_checkbox');3. Let the blog administrator decide under what circumstances to send emailsAdd the following function between the tags in the functions.php file:/* comment_mail_notify v1.0 by willin kan. (无勾选栏) */
function comment_mail_notify($comment_id) {
$admin_email = get_bloginfo ('admin_email'); // $admin_email 可改为你指定的 e-mail.
$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) && ($comment_author_email == $admin_email)) {
/* 上面的判断式,决定发出邮件的必要条件:
($parent_id != '') && ($spam_confirmed != 'spam'): 回复的, 而且不是 spam 才可发, 必需!!
($to != $admin_email) : 不发给 admin.
($comment_author_email == $admin_email) : 只有 admin 的回复才可发.
可视个人需修改上面的条件.
*/
$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 发出点, no-reply 可改为可用的 e-mail.
$subject = '您在 [' . get_option("blogname") . '] 的留言有了回复';
$message = '
' . trim(get_comment($parent_id)->comment_author) . ', 您好!
您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:
'
. trim(get_comment($parent_id)->comment_content) . '
' . trim($comment->comment_author) . ' 给您的回复:
'
. trim($comment->comment_content) . '
您可以点击 查看回复的完整內容
还要再度光临 ' . get_option('blogname') . '
(此邮件由系统自动发送,请勿回复.)
';
$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 );
//echo 'mail to ', $to, '
' , $subject, $message; // for testing
}
}
add_action('comment_post', 'comment_mail_notify');
// -- END ----------------------------------------Those interested in the code canclick here to visit Shenjie's blog, which is written in considerable detail. However, this code may not work with some Themes, so Naiba recommends using the Wenprise Better Emails WordPress comment email notification plugin as a solution.Plugin Method (Recommended)
Wenprise Better Emails can beautify WordPress comment moderation notification emails, comment notification emails to the article author, and add the function of sending email notifications to the comment author when replying to comments. Naiba recommends the Wenprise Better Emails plugin for the following reasons:- It implements comment email reply notifications, works out of the box, and is suitable for beginners;
- It's a plugin made by a Chinese developer, aligns with usage habits, and has a nice interface;
- It includes an unsubscribe function, which can reduce the chance of emails being marked as spam.

Comments are closed
The comment function for this article is closed. If you have any questions, please feel free to contact us through other channels.