wordpress 3.8 不使用插件,实现email通知访客其评论有人回复

作者: 站长 上传时间: 浏览: N/A 下载: N/A 格式: N/A 评分: N/A

使用插件实现太容易了,但是我们想让自己的博客飞起来就少用插件吧。
不使用插件提供通知访客其评论有回复的前提条件是服务器开启了mail()函数,linux的虚拟主机一般都开启了的,没有开启需要找客服帮忙。如果是 linux VPS可以自己开启,方法如下。

一、服务器已经开启了mail()函数的情况下(未开启请看第二大段):
进入wordpress后台“外观”—->“编辑”—->右边找到“functions.php”—->在下一行回车,空出一行后,然后粘贴以下代码,并保存即可:
//comment_mail_notify(所有的回复都会发邮件通知)
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地址
$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');

二、linux服务器开启mail()函数的方法
先是ssh连接上VPS,然后输入以下命令vi /usr/local/php/etc/php.ini
在命令模式,使用VI的查找命令“/”进行内容查找:
/mail
如果第一次没有查找到,请再次输入/mail查找,直到找到[mail function]
位置,会看看到以下代码:
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = [email protected]

; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path =

然后将sendmail_path =修改为:
sendmail_path = /usr/sbin/sendmail -t -i
然后重启php-fpm进程
/etc/init.d/php-fpm restart

以上步骤完了过后如果发现还不行,那可能是安装或启动sendmail 组件,那么按以下步骤重新安装:
安装组件:
yum install sendmail
重启进程
/etc/init.d/php-fpm restart
检查运行情况
/etc/init.d/sendmail status
如果现实running则安装运行成功。
sendmail启动停止重启命令,根据需要选择后面的star、stop、restart
/etc/init.d/sendmail start stop restart

2 thoughts on “wordpress 3.8 不使用插件,实现email通知访客其评论有人回复

Leave a Comment