WordPress教程

更新WordPress文章通知评论过的用户

阿里云

我们使用 WordPress 建站时,时常会发布一些代码类、持续更新类的文章,那么会有存在更新和追加的情况,这样的话我们需要通知那些评论过这篇文章这的用户呢。这段代码就是实现在文章有修改或更新时候可以决定是否邮件通知对这篇文章有评论的访客。如果对一些类似的旧文章进行更新,而这种更新一旦没有及时通知感兴趣的朋友,往往就没有什么意义了。那么怎么存代码实现更新 WordPress 文章通知评论过的用户呢?我们需要添加相关代码后会在修改编辑文章的页面添加一个选项,勾选这个选项才会邮件通知评论过的用户,这样可以明显减轻回复的压力,只有勾选才会邮件通知,这样就不会每次修改频繁打扰到网友。

实现方法:

将以下代码放到 functions.php 中:

也想出现在这里?联系我们
创客主机
  1. /**
  2.  * Wordpress 修改更新文章后邮件通知评论过那篇文章的用户
  3.  */
  4. //修改更新文章时邮件通知评论用户
  5. add_action( 'submitpost_box', 'lxtx_fo_submit_box');
  6. function lxtx_fo_submit_box( ){
  7. echo '<div id="fo_side-sortables" class="meta-box-sortables ui-sortable">';
  8. echo '<div id="fo_submit_box" class="postbox ">';
  9. echo '<div class="handlediv" title="点击以切换"><br></div>';
  10. echo '<h3 class="hndle"><span>邮件通知</span></h3>';
  11. echo '<div class="inside"><div class="submitbox">';
  12. echo ' <div style="padding: 10px 10px 0;text-align: left;"><label class="selectit" title="慎用此功能,重要文章才勾选嘛,以免引起读者反感哈"><input type="checkbox" name="FO_emaill_report_user" value="true" title="勾选此项,将邮件通知本文所有评论者"/>邮通知本文所有评论者</label></div>';
  13. echo '</div></div>';
  14. echo '</div>';
  15. echo '</div>';
  16. }
  17. //开始
  18. add_action( 'publish_post', 'lxtx_fo_emaill_report_users' );
  19. function lxtx_fo_emaill_report_users($post_ID)
  20. {
  21. //如果未勾选保存,不进行任何操作
  22. if($_POST['FO_emaill_report_user'] != 'true'){
  23. return;
  24. }
  25.  
  26. //修订版本不通知,以免滥用
  27. if( wp_is_post_revision($post_ID) ){
  28. return;
  29. }
  30.  
  31. //获取wp数据操作类
  32. global $wpdb,$post;
  33. // 读数据库,获取文章的所有用户的email并且不重复
  34. $emailauthor != '你自己的邮箱';
  35. $wp_user_emails = $wpdb->get_results("SELECT DISTINCT comment_author, comment_author_email FROM $wpdb->comments WHERE TRIM(comment_author_email) IS NOT NULL AND TRIM(comment_author_email) != '' AND TRIM(comment_author_email) != '$emailauthor' AND comment_post_ID = $post->ID");
  36.  
  37. // 获取博客名称
  38. $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  39. // 获取博客URL
  40. $blogurl = get_bloginfo("siteurl");
  41.  
  42. //文章链接
  43. $post_link = get_permalink($post_ID);
  44. //文章标题$post -> post_title
  45. $post_title = strip_tags($_POST['post_title']);
  46. //文章内容$post->post_content
  47. $post_content = strip_tags($_POST['post_content']);
  48. //文章摘要
  49. $output = preg_replace('/^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,0}((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,200}).*/s','\1',$post_content).'......';
  50.  
  51. //邮件头,以免乱码
  52. $message_headers = "Content-Type: text/HTML; charset=\"utf-8\"\n";
  53. // 邮件标题
  54. $subject = '您曾经来访过的《'.$post_title.'》文章更新通知。';
  55.  
  56. foreach ( $wp_user_emails as $wp_user_email )
  57. {
  58. // 邮件内容
  59. $message = '
  60. <div style="MARGIN-RIGHT: auto; MARGIN-LEFT: auto;">
  61. <strong style="line-height: 1.5; font-family:Microsoft YaHei;">
  62. 亲爱的'.$wp_user_email->comment_author.':
  63. </strong>
  64. <p style="FONT-SIZE: 14px; PADDING-TOP: 6px">
  65. 您曾经来访过的《'.$post_title.'》有更新,博主觉得有必要通知您,希望不会骚扰到您。
  66. </p>
  67. <p style="FONT-SIZE: 14px; PADDING-TOP: 6px">
  68. 文章标题:<a title="'.$post_title.'" href="'.$post_link.'" target="_top">'.$post_title.'</a>
  69. <br/>
  70. 文章摘要:'.$output.'
  71. </p>
  72. <p style="FONT-SIZE: 14px; PADDING-TOP: 6px">
  73. 您可以点击链接
  74. <a href="'.$blogurl.'" style="line-height: 1.5;">'.$blogname.'</a>
  75. >
  76. <a title="'.$post_title.'" href="'.$post_link.'" target="_top">'.$post_title.'</a>
  77. 详细查看
  78. </p>
  79. <p style="font-size: 14px; padding-top: 6px; text-align: left;">
  80. <span style="line-height: 1.5; color: rgb(153, 153, 153);">
  81. 来自:
  82. </span>
  83. <a href="'.$blogurl.'" style="line-height: 1.5;">'.$blogname.'</a>
  84. </p>
  85. <div style="font-size: 12px; border-top-color: rgb(204, 204, 204); border-top-width: 1px; border-top-style: solid; height: 35px; width: 500px; color: rgb(102, 102, 102); line-height: 35px; background-color: rgb(245, 245, 245);">
  86. 该邮件为系统发送邮件,请勿直接回复!如有打扰,请向博主留言反映。灰常感谢您的阅读!
  87. </div>
  88. </div>';
  89. wp_mail($wp_user_email->comment_author_email, $subject, $message, $message_headers);
  90. }
  91. }

更新 WordPress 文章通知评论过的用户

已有 298 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!