博客网站有很多,有时候别人在你的网站上评论后,可能很快就会忘了,所以当有人评论回复是邮件提醒对方,就显得很有用了。很多博客都有评论回复的功能,要实现这个功能也很简单,下面就提供一个相对简单的模板。
首先是看效果图:
实现代码
要实现这个功能,将以下的代码,添加在主题的 functions.php 文件的 最后一个 ?> 前面即可:
/**
* WordPress 评论回复邮件通知
* //www.capjsj.cn
* DIY By 成都航院计算机工程系
**/
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') . "\" ";
$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');
/* 自动加勾选栏 */
function add_checkbox() {
echo '有人回复时邮件通知我';
}
add_action('comment_form', 'add_checkbox');
备注说明
发送邮件,需要主机支持 mail() 函数,如果你发现没办法收到邮件,可以询问你的主机商。由于每个人的主机环境不一样,有些朋友在添加这个功能的时候,总是不能成功,这时候,你可以试试 SMTP 发送邮件的方式。
本文参考自:http://www.wpdaxue.com/comment-mail-notify.html,由狂族晨曦(capjsj.cn)整理编辑,本文共3076个字。
本文链接地址:https://www.capjsj.cn/wordpress_plhfyjtx_jdmb.html , 转载请保留本说明!