奥多码,点击查看详情 APP分发吧,点击查看详情

WordPress非插件实现评论回复邮件提醒通知

       
广告2021-06-03到期2021-07-03广告2021-06-03到期2021-07-03
       
广告2021-06-03到期2021-07-03广告2021-06-03到期2021-07-03

今天给博客添加了评论回复邮件提醒通知功能,访客可自主选择是否开启此功能。效果如下图,方法来源于:wp大学,感谢原作者的教程。

WordPress非插件实现评论回复邮件提醒通知

WordPress 要想在第一时间知道有人在自己博客留言,然后在第一时间回复留言再第一时间通知邮件者,这就需要WordPress博客拥有邮件通知功能。这个功能可以使用相关插件实现,但是试用过几个评论回复邮件通知插件,但都不太满意。

操作请注意:

1.一定不要使用记事本编辑php文件,否则很可能出现意想不到的错误,推荐使用UltraEdit,Dreamweaver等。
2.如果对于修改代码不是很有把握,请预先备份好原来的文件。
3.复制本文的代码,如果是全角符号,请自行改为半角符号,切记。

评论邮件通知的方法:

1.所有回复都发送邮件通知

登陆博客后台,点击“外观”选项卡下的“编辑”选项进入主题编辑界面,在functions.php文件中的<?php和?&gt;之间添加以下函数即可:

  1. /*comment_mail_notifyv1.0bywillinkan.(所有回复都发邮件)*/
  2. functioncomment_mail_notify($comment_id){
  3. $comment=get_comment($comment_id);
  4. $parent_id=$comment->comment_parent?$comment->comment_parent:;
  5. $spam_confirmed=$comment->comment_approved;
  6. if(($parent_id!=)&&($spam_confirmed!=‘spam’)){
  7. $wp_email=‘[emailprotected]’.preg_replace(‘#^www.#’,,strtolower($_SERVER[‘SERVER_NAME’]));//e-mail发出点,no-reply可改为可用的e-mail.
  8. $to=trim(get_comment($parent_id)->comment_author_email);
  9. $subject=‘您在[‘.get_option(“blogname”).‘]的留言有了回复’;
  10. $message=‘
  11. <divstyle=“background-color:#eef2fa;border:1pxsolid#d8e3e8;color:#111;padding:015px;-moz-border-radius:5px;-webkit-border-radius:5px;-khtml-border-radius:5px;”>
  12. <p>’.trim(get_comment($parent_id)->comment_author).‘,您好!</p>
  13. <p>您曾在《’.get_the_title($comment->comment_post_ID).‘》的留言:<br/>’
  14. .trim(get_comment($parent_id)->comment_content).‘</p>
  15. <p>’.trim($comment->comment_author).‘给您的回复:<br/>’
  16. .trim($comment->comment_content).‘&lt;br/></p>
  17. <p>您可以点击查看回复完整內容</p>
  18. <p>欢迎再度光临‘.get_option(‘blogname’).‘</p>
  19. <p>(此邮件由系统自动发送,请勿回复.)</p>
  20. </div>’;
  21. $from=“From:””.get_option(‘blogname’).“”<$wp_email>”;
  22. $headers=“$fromnContent-Type:text/html;charset=”.get_option(‘blog_charset’).“n”;
  23. wp_mail($to,$subject,$message,$headers);
  24. //echo‘mailto‘,$to,‘<br/>‘,$subject,$message;//fortesting
  25. }
  26. }
  27. add_action(‘comment_post’,‘comment_mail_notify’);
  28. //—END—————————————-

2.让访客自己选择是否邮件通知

在functions.php文件中的<?php和?>之间添加以下函数,该函数将会在评论框底部生成要不要收回复通知的选项:

  1. /*开始*/
  2. functioncomment_mail_notify($comment_id){
  3. $admin_notify=‘1’;//admin要不要收回复通知(‘1’=要;‘0’=不要)
  4. $admin_email=get_bloginfo(‘admin_email’);//$admin_email可改为你指定的e-mail.
  5. $comment=get_comment($comment_id);
  6. $comment_author_email=trim($comment->comment_author_email);
  7. $parent_id=$comment->comment_parent?$comment->comment_parent:;
  8. global$wpdb;
  9. if($wpdb->query(“Describe{$wpdb->comments}comment_mail_notify”)==)
  10. $wpdb->query(“ALTERTABLE{$wpdb->comments}ADDCOLUMNcomment_mail_notifyTINYINTNOTNULLDEFAULT0;”);
  11. if(($comment_author_email!=$admin_email&&amp;isset($_POST[‘comment_mail_notify’]))||($comment_author_email==$admin_email&amp;&$admin_notify==‘1’))
  12. $wpdb->query(“UPDATE{$wpdb->comments}SETcomment_mail_notify=’1′WHEREcomment_ID=’$comment_id’”);
  13. $notify=$parent_id?get_comment($parent_id)->comment_mail_notify:‘0’;
  14. $spam_confirmed=$comment->comment_approved;
  15. if($parent_id!=&amp;&$spam_confirmed!=‘spam’&&$notify==‘1’){
  16. $wp_email=‘[emailprotected]’.preg_replace(‘#^www.#’,,strtolower($_SERVER[‘SERVER_NAME’]));//e-mail发出点,no-reply可改为可用的e-mail.
  17. $to=trim(get_comment($parent_id)->comment_author_email);
  18. $subject=‘您在[‘.get_option(“blogname”).‘]的留言有了回复’;
  19. $message=‘
  20. <divstyle=“background-color:#eef2fa;border:1pxsolid#d8e3e8;color:#111;padding:015px;-moz-border-radius:5px;-webkit-border-radius:5px;-khtml-border-radius:5px;”>
  21. <p>’.trim(get_comment($parent_id)->comment_author).‘,您好!</p>
  22. <p>您曾在《’.get_the_title($comment->comment_post_ID).‘》的留言:<br/>’
  23. .trim(get_comment($parent_id)->comment_content).‘&lt;/p>
  24. <p&gt;’.trim($comment->comment_author).‘给您的回复:<br/>’
  25. .trim($comment->comment_content).‘<br/&gt;</p>
  26. <p>您可以点击查看回复的完整內容</p>
  27. <p>还要再度光临‘.get_option(‘blogname’).‘</p>
  28. <p>(此邮件由系统自动发送,请勿回复.)</p>
  29. </div>’;
  30. $from=From:””.get_option(‘blogname’).“”<$wp_email>”;
  31. $headers=“$fromnContent-Type:text/html;charset=”.get_option(‘blog_charset’).“n”;
  32. wp_mail($to,$subject,$message,$headers);
  33. //echo‘mailto‘,$to,‘<br/>‘,$subject,$message;//fortesting
  34. }
  35. }
  36. add_action(‘comment_post’,‘comment_mail_notify’);
  37. /*自动加勾选栏*/
  38. functionadd_checkbox(){
  39. echo‘<inputtype=“checkbox”name=“comment_mail_notify”id=“comment_mail_notify”value=comment_mail_notify”checked=“checked”style=“margin-left:20px;”/><labelfor=“comment_mail_notify”&gt;有人回复时邮件通知我</label>’;
  40. }
  41. add_action(‘comment_form’,‘add_checkbox’);

3.让博客管理员决定什么情况下发邮件

在functions.php文件中的<?php和?>之间添加以下函数:

  1. /*comment_mail_notifyv1.0bywillinkan.(无勾选栏)*/
  2. functioncomment_mail_notify($comment_id){
  3. $admin_email=get_bloginfo(‘admin_email’);//$admin_email可改为你指定的e-mail.
  4. $comment=get_comment($comment_id);
  5. $comment_author_email=trim($comment>comment_author_email);
  6. $parent_id=$comment->comment_parent?$comment->comment_parent:;
  7. $to=$parent_id?trim(get_comment($parent_id)->comment_author_email):;
  8. $spam_confirmed=$comment->comment_approved;
  9. if(($parent_id!=)&&($spam_confirmed!=‘spam’)&&($to!=$admin_email)&&($comment_author_email==$admin_email)){
  10. /*上面的判断式,决定发出邮件的必要条件:
  11. ($parent_id!=”)&&($spam_confirmed!=‘spam’):回复的,而且不是spam才可发,必需!!
  12. ($to!=$admin_email):不发给admin.
  13. ($comment_author_email==$admin_email):只有admin的回复才可发.
  14. 可视个人需修改上面的条件.
  15. */
  16. $wp_email=‘[emailprotected]’.preg_replace(‘#^www.#’,,strtolower($_SERVER[‘SERVER_NAME’]));//e-mail发出点,no-reply可改为可用的e-mail.
  17. $subject=‘您在[‘.get_option(“blogname”).‘]的留言有了回复’;
  18. $message=‘
  19. <divstyle=“background-color:#eef2fa;border:1pxsolid#d8e3e8;color:#111;padding:015px;-moz-border-radius:5px;-webkit-border-radius:5px;-khtml-border-radius:5px;”>
  20. &lt;p>’.trim(get_comment($parent_id)->comment_author).‘,您好!</p>
  21. <p>您曾在《’.get_the_title($comment->comment_post_ID).‘》的留言:<br/>’
  22. .trim(get_comment($parent_id)->comment_content).‘</p>
  23. <p>’.trim($comment>comment_author).‘给您的回复:<br/>’
  24. .trim($comment->comment_content).‘<br/>&lt;/p>
  25. <p>您可以点击查看回复的完整內容</p&gt;
  26. <p>还要再度光临‘.get_option(‘blogname’).‘</p>
  27. <p>(此邮件由系统自动发送,请勿回复.)</p>
  28. </div>’;
  29. $from=“From:””.get_option(‘blogname’).“”<$wp_email&gt;”;
  30. $headers=“$fromnContent-Type:text/html;charset=”.get_option(‘blog_charset’).“n”;
  31. wp_mail($to,$subject,$message,$headers);
  32. //echo‘mailto‘,$to,‘<br/>‘,$subject,$message;//fortesting
  33. }
  34. }
  35. add_action(‘comment_post’,‘comment_mail_notify’);
  36. //—END—————————————-

你可以根据需要选择代码,根据主题的不同可能要略作修改。本站使用的是第二种。

本文由【好易之】整理自网络!
原创文章,作者:【好易之】如转载请注明出处:https://www.zhengjiaxi.com/zxwd/jswd/69677.html
如有侵权,请邮件联系 aoduoye@qq.com 删除。
本站发布的文章及附件仅限用于学习和研究目的;不得将上述内容用于商业或非法用途,否则后果请用户自负。
本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。
如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。
(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
好易之的头像好易之站长
上一篇 2020-05-27 00:32
下一篇 2020-05-27 00:32

相关推荐

发表回复

登录后才能评论

联系我们

400-800-8888

在线咨询:点击这里给我发消息

 

工作时间:周一至周五,9:30-18:30,节假日休息

关注公众号
请查看头部文章来源地址!本站所有内容均为互联网收集整理和网友上传。仅限于学习研究,切勿用于商业用途。否则由此引发的法律纠纷及连带责任本站概不承担。
阿里云