评论回复邮件通知插件 Comment Email Reply

今天下午登陆网站无意发现网站的邮件功能失效了,毫无疑问,最近这段时间的评论回复邮件没能发出去。问题很快被找到,原来是 WP Mail SMTP 插件被重置了,重新做了设置,又把当前在用的评论回复邮件通知插件 Comment Email Reply 做了一番改动

改动内容

  1. 使之兼容最新版的 WordPress,原插件已不能在新版的WP 上正常工作了;
  2. 评论回复通知邮件延迟发送,默认延迟300秒;
  3. 将评论设置为 spam 或删除评论时也会向原评论人发送通知邮件;

使用方法

  1. 请确认网站的邮件功能正常,可借助于 WP Mail SMTP 插件来实现邮件功能
  2. 将下方代码复制并保存为 php 后缀文件,如 comment_email_reply.php
  3. 在网站目录 wp-content/plugins 下新建一个文件夹,如 Comment Email Reply,将上一步所建的 php 文件放入其中
  4. 登陆网站后台,在插件页面激活插件 Comment Email Reply 即可

以下是修改后的代码:

<?php
/**
* Plugin Name: Comment Email Reply
* Plugin URI:
* Description:
* Version: 1.0.5
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/

add_filter('wp_mail_content_type', function($contentType) { return 'text/html'; });
function cer_comment_notification($comment_ID) {
$comment_object = get_comment($comment_ID);
$comment_parent = get_comment($comment_object->comment_parent);

$mail_dtl = '<html><head><style>#mgy{margin:0;padding:0;font-size:16px;font-size:1rem;line-height:1.75;color:#444;background-color:#f6f6f6}#mgy a{color:#059fda}#mgy .a{padding:40px 15px}#mgy .b{width:600px;margin:0 auto;padding:40px;border-radius:10px;background:#fff}#mgy .c{clear:both}#mgy .d{margin:0 0 24px}#mgy .e{border-top:1px solid #e7e7e7;padding-top:10px;}#mgy .f{margin-bottom:10px}#mgy .g{margin:0 5px}#mgy .h{width:100px;height:32px;line-height:32px;padding:2px;border-radius:32px;color:#fff;background:#059fda}#mgy .h:hover{background:#00b1dc}#mgy .i{margin:40px 0 0}#mgy .s{border-bottom:1px dotted;text-decoration:none}#mgy .k{text-align:center;text-decoration:none}#mgy .l{display:block}#mgy .o{margin:5px 0}#mgy .p{color:#5a5a5a}#mgy .q{background:#f5f5f5;border-radius:4px;padding:8px;line-height:1.5}#mgy .z{width:212px;height:84px;margin:0 auto 40px;background-image:url("https://static.muguayuan.com/assets/mail.gif");background-repeat:no-repeat}@media screen and (max-width:680px){#mgy{font-size:18px;font-size:1.125rem;line-height:2}#mgy .b{width:100%;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box}}</style></head><body>';
$mail_dtl .= '<div id="mgy" style="background-color:#f1f1f1">';
$mail_dtl .= '<div class="a"><div class="b"><div class="l z"></div><div class="o">';

if ($comment_object->comment_approved == 1 && $comment_object->comment_parent > 0) {

/**特色图
*if (has_post_thumbnail( $comment_parent->comment_post_ID ) ){
* $mail_dtl .= ' style="background-image:url(';
* $mail_dtl .= get_the_post_thumbnail_url($comment_parent->comment_post_ID, 'medium' );
* $mail_dtl .= ');background-repeat:no-repeat;background-position:50% 50%;background-size:cover"';
*}
**/

$mail_dtl .= $comment_parent->comment_author . ':</div>';
$mail_dtl .= '<div class="f">您好,您在<a class="g s" href="' . get_permalink($comment_parent->comment_post_ID) . '">' . get_the_title($comment_parent->comment_post_ID) . '</a>一文中发表的评论</div>';

$mail_dtl .= '<div class="f q">' . esc_html($comment_parent->comment_content) . '</div>';
$mail_dtl .= '<div class="f">被<span class="g">';
//作者URL
if ($comment_object->comment_author_url !== '') {
$mail_dtl .= '<a class="s" href="' . $comment_object->comment_author_url . '">';
$mail_dtl .= $comment_object->comment_author;
$mail_dtl .= '</a>';
} else {
$mail_dtl .= $comment_object->comment_author;
}

$mail_dtl .= '</span>作了如下回复</div>';
$mail_dtl .= '<div class="d q">'. esc_html($comment_object->comment_content) . '</div>';
$mail_dtl .= '<a class="h k l" href="' . get_comment_link( $comment_parent->comment_ID ) . '">继续回复</a>';

$email = $comment_parent->comment_author_email;
$title ='来自 ['. get_option('blogname') . '] 的评论回复';
}

elseif ($comment_object->comment_approved == 'spam') {

$mail_dtl .= $comment_object->comment_author . ':</div>';
$mail_dtl .= '<div class="f">您在<a class="g s" href="' . get_permalink($comment_object->comment_post_ID) . '">' . get_the_title($comment_object->comment_post_ID) . '</a>一文<span class="f">发表的评论</span></div>';
$mail_dtl .= '<div class="f q">' . esc_html($comment_object->comment_content) . '</div>';
$mail_dtl .= '<div class="f">已被系统识别为垃圾评论</div>';
$mail_dtl .= '<div class="f">请不要发送垃圾评论,谢谢。</div>';

$email = $comment_object->comment_author_email;
$title ='您在 ['. get_option('blogname') . '] 的评论状态发生改变';
}

elseif ($comment_object->comment_approved == 'trash') {

$mail_dtl .= $comment_object->comment_author . ':</div>';
$mail_dtl .= '<div class="f">很抱歉地通知您,您在<a class="g s" href="' . get_permalink($comment_object->comment_post_ID) . '">' . get_the_title($comment_object->comment_post_ID) . '</a>一文<span class="f">发表的评论</span></div>';
$mail_dtl .= '<div class="f q">' . esc_html($comment_object->comment_content) . '</div>';
$mail_dtl .= '<div class="f">已被管理员无情地删除</div>';

$email = $comment_object->comment_author_email;
$title ='您在 ['. get_option('blogname') . '] 的评论已被删除';
}

$mail_dtl .= '<div class="f i e">欢迎订阅 [木瓜园] https://muguayuan.com/feed</div><div class="p">本邮件由系统自动发送,请勿直接回复,谢谢!</div></div></div></div></body></html>';
$mailcontent = $mail_dtl;
$subject = $title;
wp_mail($email, $subject, $mailcontent);
}
add_action('comment_mail_notify','cer_comment_notification');

function comment_mail_notify_schedule($comment_ID) {
wp_schedule_single_event( time()+300, 'comment_mail_notify', array($comment_ID));
}
add_action('wp_insert_comment','comment_mail_notify_schedule',99,2);

function cer_comment_status_changed($comment_ID, $comment_status) {
$comment_object = get_comment($comment_ID);
if ($comment_status == 'approve' || 'spam' || 'trash') {
cer_comment_notification($comment_object->comment_ID, $comment_object);
}
}
# Fire Email when comments gets approved later.
add_action('wp_set_comment_status','cer_comment_status_changed',99,2);
?>

发表评论

评论(1)

  1. 老虎

    收藏了