wordpress邮件回复评论解决方案

评论邮件通知是wordpress比较重要的功能,本博客以前的邮件回复插件失效了,多方寻求答案无果,结果在优旁博客找到了一段代码,解决了问题,在此分享下。

使用方法:
安装wp smtp插件,配置好smtp发信后,编辑主题的函数文件functions,在最底部加入以下代码,根据实际情况来修改代码

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 = '' . 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 = ' 
 <div style="width: 60%;margin: 0 auto"> 
 <div style="font-size: 28px;line-height: 28px;text-align: center;"><p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p></div> 
 <div style="border-bottom: 1px solid #eee;padding-top: 10px;"> 
 <p style="color: #999;">您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:</p> 
 <p style="font-size: 18px;">' . trim(get_comment($parent_id)->comment_content) . '</p> 
 </div> 
 <div style="border-bottom: 1px solid #eee;padding-top: 10px;"> 
 <p style="color: #999;">' . trim($comment->comment_author) . ' 给您的回复:</p> 
 <p style="font-size: 18px;">' . trim($comment->comment_content) . '</p>
 <p style="text-align: center;font-size: 12px;padding-bottom: 20px;"><a style="border: 1px solid #3297fb;color: #3297fb;padding: 7px 14px;text-decoration: none;-moz-border-radius: 4px;-webkit-border-radius: 4px;border-radius:4px;" href="' . esc_attr(get_comment_link($parent_id, array('type' => 'comment'))) . '">点击查看</a></p> 
 </div> <div style="font-size: 12px;color: #999;text-align: center;"> 
 <p>此邮件由系统自动发送,请勿回复</p> 
 <p>© <a href="http://www.slll.info" style="color: #999;text-decoration: none;">' . get_option('blogname') . '</a></p> 
 </div> 
 </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 ); } } 
add_action('comment_post', 'comment_mail_notify');

Leave a Reply

Your email address will not be published. Required fields are marked *