闻心阁

一蓑烟雨看苍生,半壶浊酒笑红尘

wordpress加入评论邮件回复

2012-03-19 约 1 分钟读完 搬砖秘籍

可能我们在访问别人的wordpress博客的时候经常会收到一些类似"你在XXX的评论有新的回复"的邮件,也有的写的很好玩,现在在写新的主题,也就把这个功能集成进去了,代码如下~

/* 邮件通知 by Qiqiboy */
 function comment_mail_notify($comment_id) {
     $comment = get_comment($comment_id);
     $content=$comment->comment_content;
     $match_count=preg_match_all('/<a href="#comment-([0-9]+)?" rel="nofollow">/si',$content,$matchs);
     if($match_count>0){
         foreach($matchs[1] as $parent_id){
             SimPaled_send_email($parent_id,$comment);
         }
     }elseif($comment->comment_parent!='0'){
         $parent_id=$comment->comment_parent;
         SimPaled_send_email($parent_id,$comment);
     }else return;
 }
 add_action('comment_post', 'comment_mail_notify');
 function SimPaled_send_email($parent_id,$comment){
     $admin_email = get_bloginfo ('admin_email');
     $parent_comment=get_comment($parent_id);
     $author_email=$comment->comment_author_email;
     $to = trim($parent_comment->comment_author_email);
     $spam_confirmed = $comment->comment_approved;
     if ($spam_confirmed != 'spam' && $to != $admin_email && $to != $author_email) {
         $wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
         $subject = '您在 [' . get_option("blogname") . '] 的留言有了回應';
         $message = '<div style="background-color:#eef2fa;border:1px solid #d8e3e8;color:#111;padding:0 15px;-moz-border-radius:5px;-webkit-border-radius:5px;-khtml-border-radius:5px;">
             <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
             <p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />'
             . trim(get_comment($parent_id)->comment_content) . '</p>
             <p>' . trim($comment->comment_author) . ' 给你的回复:<br />'
             . trim($comment->comment_content) . '<br /></p>
             <p>您可以点击 <a href="' . htmlspecialchars(get_comment_link($parent_id,array("type" => "all"))) . '">查看回复的完整內容</a></p>
             <p>欢迎再度光临 <a href="' . get_option('home') . '">' . get_option('blogname') . '

<p>(此邮件有系统自动发出, 请勿回复.)</p></div>'; $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 ); } } function add_checkbox() { echo '<span id="cb"><input type="checkbox" name="comment_mail_notify" id="comment_mail_notify" value="comment_mail_notify" checked="checked" style="margin-top:20px;" /><label for="comment_mail_notify" style="color: #888;">' . __('有人回复通知我') . '</label></span>'; } add_action('comment_form', 'add_checkbox');

上面的代码来自这里这里,非本人所写。感谢这些牛人贡献的代码。