As is known to all, the more plugins you use, the more memory is required in WordPress. The best way to implement different functions is to add the core snippets of the code directly by yourself, so you know which part is necessary and which part is not. In this tutorial I'm going to introduce 2 ways to notify WordPress comments author upon their replies without plugins. (The core codes is written by "willin kan" in few years ago, not me =_=)
Final result: When visitor A leave a comment on the WordPress site, any future replies to this comment will triggers the email mechanism which has been pretested, the WordPress site will then sends email to visitor A based on the email address as notification. The following figure shows the final result:
Okay, let's get started!
Method 1: Let comment author to decide whether send email to notify replies or not.
* mail() function is required on the server |
Some server has disabled mail() function in order to prevent spams. You have to check is this function turning on or off, by checking this status, simply go to the back-end of your WordPress site and click on "Lost your password?" try to find password via email notification. If the WordPress is not able to send an email in this way, it means you have to go to Method 2, otherwise, you can continue to the next step:
Open up the file, find a proper place (before the last "?>") to insert the following code:
/* comment_mail_notify v1.0 by willin kan. */ function comment_mail_notify($comment_id) { $admin_notify = '1'; // Is admin gonna receive notification? ('1'=yes; '0'=no) $admin_email = get_bloginfo ('admin_email'); // $admin_email can be replaced by a proper email address $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 sender, no-reply can be replaced by an email address $to = trim(get_comment($parent_id)->comment_author_email); $subject = 'Subscription: New reply | ゞ(≧▽≦*)o'; $message = ' <div>New reply from <u>' . trim($comment->comment_author) . '</u> on <b>' . get_the_title($comment->comment_post_ID) . '</b><br> View comment: <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">' . htmlspecialchars(get_comment_link($parent_id)) . '</a></div> <p>// This is an automatically generated email, please do not reply.</p>'; $from = "From: "" . get_option('blogname') . "" < $wp_email>"; $headers = "$fromnContent-Type: text/html; charset=" . get_option('blog_charset') . "n"; wp_mail( $to, $subject, $message, $headers ); // echo 'mail to ', $to, '<br /> ' , $subject, $message; // for testing } } add_action('comment_post', 'comment_mail_notify'); /* Tick the checkbox by default */ function add_checkbox() { echo '<input type="checkbox" name="comment_mail_notify" id="comment_mail_notify" value="comment_mail_notify" checked="checked" style="margin-left:20px;" /><label for="comment_mail_notify">Notify me when my comments has new reply</label>'; } add_action('comment_form', 'add_checkbox');Save it and refresh your WordPress site, you can find a "checkbox" below the comment form, and the function has completed. Now make some comments and reply them for a test! - Known issue #1: The checkbox will not appears on some mobile theme, so the comment notification will probably not working in this case.
Method 2: No checkbox, notify comment author when someone replies their comments by default.
This method is using SMTP() function on server, it is more likely to be effective. To do so, Open up the functions.php file, find a proper place (before the last "?>") to insert the following code:// SMTP function mail_smtp( $phpmailer ) { $phpmailer->FromName = 'BianLei.com'; // Sender name, can be anything you want $phpmailer->Host = 'smtp.gmail.com '; // smtp server, should be same as your email service provider $phpmailer->Port = 25; // Refer to your email service provider $phpmailer->Username = 'xxx@gmail.com';// Replace with your email address $phpmailer->Password = 'password';// Replace with your email password $phpmailer->From = 'xxx@gmail.com'; // Replace with your email address $phpmailer->SMTPAuth = true; $phpmailer->SMTPSecure = 'ssl'; // Put 'tls' or 'ssl' or even leave as blank if non of them, this is also provided by email service provider, you can search them out. $phpmailer->IsSMTP(); } add_action('phpmailer_init', 'mail_smtp'); // Send Email function comment_mail_notify($comment_id) { $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)) { /* Change to the next line of code if you want receive comment reply notification from admin only if (($parent_id != '') && ($spam_confirmed != 'spam') && ($to != $admin_email) && ($comment_author_email == $admin_email)) { */ $subject = 'Subscription: New reply | ゞ(≧▽≦*)o'; $message = ' <div>New reply from <u>' . trim($comment->comment_author) . '</u> on <b>' . get_the_title($comment->comment_post_ID) . '</b><br> View comment: <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">' . htmlspecialchars(get_comment_link($parent_id)) . '</a></div> <p>// This is an automatically generated email, please do not reply.</p>'; $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, '<br/> ' , $subject, $message; // For testing } } add_action('comment_post', 'comment_mail_notify'); // FinishReview the code and config it at where the code annotation as. Go to your email and enable SMTP setting, then save it and it should be working well. If still cannot receive email, change the port and security setting (SSL) then try again. Reference: 免插件实现WordPress评论回复邮件通知的三种方法