最近 Disqus 被某国的墙搞的十分不稳定,于是又用回了 WordPress 自带的评论系统,但是这个评论系统却不带评论者被回复时的邮件提醒。我有自己的发信服务器(AWS SES)系统,所以理论上可以配合插件实现这个效果。但是我看了很多插件,基本上操作页面都太复杂,而且回复的邮件通常不支持中文,我只需要一个简单的回复系统,不用那么麻烦,于是干脆自己开发一个,最终比较完美的实现了这个功能。
我开发的这个功能特点是:当评论者被回复时,邮件标题是 “Re: [文章标题]”,这样评论者的一个留言被多个人回复时,会被自动在本地邮件客户端上归为一类;而且评论者收到邮件后可以直接回复邮件,会直接给回复的发出者发邮件(不会显示在网站上,我也无法看到,这将是两人的私聊)。
邮件内容简洁,无额外无用的东西,不会被认定为 Spam。
[img id=”1926″ size=”medium”]收件例子[/img]
所有代码已经放到 GitHub Gist 上。
<?php function tlo_comment_mail_notify($comment_id) { global $comment_author; $comment = get_comment($comment_id); $parent_id = $comment->comment_parent ? $comment->comment_parent : ''; $spam_confirmed = $comment->comment_approved; $from = $comment->comment_author_email; $to = get_comment($parent_id)->comment_author_email; if (($parent_id != '') && ($spam_confirmed != 'spam') && $from != $to && $to != get_bloginfo('admin_email') ) { $blog_name = get_option('blogname'); $blog_url = site_url(); $post_url = get_permalink( $comment->comment_post_ID ); $comment_author = $comment->comment_author; $subject = 'Re: '.html_entity_decode(get_the_title($comment->comment_post_ID)); $headers[] = 'Reply-To: '.$comment_author.' <'.$comment->comment_author_email.'>'; $comment_parent = get_comment($parent_id); $comment_parent_date = tlo_get_comment_date( $comment_parent ); $comment_parent_time = tlo_get_comment_time( $comment_parent ); $message = <<<HTML <p>$comment->comment_content</p> <p style="color: #777; font-size: small"> — <br> Reply to this email to communicate with replier directly, or <a href="$post_url#comment-$comment_id">view it on $blog_name</a>. </p> </div> <blockquote type="cite"> <div>On {$comment_parent_date}, {$comment_parent_time},$comment_parent->comment_author <<a href="mailto: $comment_parent->comment_author_email">$comment_parent->comment_author_email</a>> wrote:</div> <br> <div class="content"> <div> <p>$comment_parent->comment_content</p> </div> </div> </blockquote> HTML; add_filter( 'wp_mail_content_type', 'tlo_mail_content_type' ); add_filter( 'wp_mail_from_name', 'tlo_mail_from_name' ); wp_mail( $to, $subject, $message, $headers ); } } add_action('comment_post', 'tlo_comment_mail_notify'); function tlo_mail_content_type() { return 'text/html'; } function tlo_mail_from_name() { global $comment_author; return $comment_author; } function tlo_get_comment_time( $comment ) { $date = mysql2date(get_option('time_format'), $comment->comment_date, true); return apply_filters( 'tlo_get_comment_time', $date, $comment ); } function tlo_get_comment_date( $comment ) { $date = mysql2date(get_option('date_format'), $comment->comment_date); return apply_filters( 'tlo_get_comment_date', $date, $comment ); }
“用回 WordPress 自带评论系统”上的13条回复
你的博客上真是满满地干货,值得收藏到RSS阅读器了。顺便一提,你博客的gravatar头像是很影响加载速度的,虽然没被墙但我这速度很慢。
感谢支持^_^
感觉图片应该不会阻塞页面加载吧?正常情况是能先显示页面,再显示图片。懒得弄反代 Gravatar 了,就用原本的也挺好。
那你的email是怎么配置的呢?是使用插件么?
使用了 Global SMTP 插件,这个插件很轻量级
我就是从 github 页面进来的
请教:wp_schedule_single_event( time(), ‘tlo_comment_post_async’, $comment_id ); 这一段加上时间就能延迟发邮件了吧?
应该可以实现的,但是 wp_schedule_single_event 后面的参数需要是 action,不是函数,所以需要创建一个action。
那有没有考虑过给你的代码加上延迟发邮件功能呢?
[…] 无意发现了ze3kr 的评论回复邮件通知代码含有执行计划任务的代码,抄过来加到在用的评论回复邮件通知插件上,这个网站的评论回复邮件通知依赖 Comment Email Reply 插件,不知从 WordPress 的哪个版本开始这个插件已经不能在这个博客正常工作了,顺便一起改了一下,下面是现在在用的完整代码: […]
经木瓜园,推荐进来了。已经订阅。? 🙂
请问你的这段代码是添加到哪里?是主题的function.php里面吗?
我是通过自定义插件的方式实现的。放在主题的 function.php 里应该也可以,但是这样的话维护起来麻烦一些。
怎么用