WordPress优化技巧:评论审核+回复+会员信息更新+新文章邮件通知系统美化.

作者 : 官方 本文共14064个字,预计阅读时间需要36分钟 发布时间: 2020-04-21 共108人阅读

WordPress优化技巧:评论审核+回复+会员信息更新+新文章邮件通知系统美化

我们都知道WordPress给用户发送的邮件是十分原生态的,没有任何的修饰以及任何的美化。通过编写静态的HTML,结合目前广为流传邮件通知代码,设计出较为美观的通知样式,如果你正好需要就拿去用吧。修改后效果如下:

WordPress 邮件通知系统美化包含功能:

1、评论审核通过通知用户

2、评论回复通知用户

3、网站后台登录失败通知管理员

4、注册用户资料信息更新通知用户

5、注册用户账户被管理员删除通知用户

6、网站发布新文章通知用户

使用方法:

将以下代码粘贴到主题的 functions.php 文件中,然后根据自己网站情况修改相应的文字内容:

  1. //邮件通知系统美化
  2. <?php
  3. //定义界面顶部区域内容,请注意修改您的主题目录
  4. $email_bg =get_bloginfo(‘url’<span%20class=”pun”>) .‘/wp-content/themes/主题目录/images/emailbg.jpg’<span%20class=”pun”>;</span%20class=”pun”></span%20class=”pun”>
  5. define (’emailbg’<span%20class=”pun”>, $email_bg );</span%20class=”pun”>
  6. $email_headertop = 
  7. <div class=”emailpaged” style=”background-image: url(‘<span%20class=”pun”>.emailbg.‘);-webkit-background-size: cover;-moz-background-size: cover;-o-background-size: cover;background-size: cover;background-position: center center;background-repeat: no-repeat;”></span%20class=”pun”>
  8. <div class=”emailcontent” style=”width:100%;max-width:720px;text-align: left;margin: 0 auto;padding-top: 80px;padding-bottom: 20px”>
  9. <div class=”emailtitle”>
  10. <h1 style=”color:#fff;background: #51a0e3;line-height:70px;font-size:24px;font-weight:normal;padding-left:40px;margin:0″>
  11. <span%20class=”pun”>;</span%20class=”pun”>
  12. define (’emailheadertop’<span%20class=”pun”>, $email_headertop );</span%20class=”pun”>
  13. $email_headerbot = 
  14. </h1>
  15. <div class=”emailtext” style=”background:#fff;padding:20px 32px 40px;”>
  16. <span%20class=”pun”>;</span%20class=”pun”>
  17. define (’emailheaderbot’<span%20class=”pun”>, $email_headerbot );</span%20class=”pun”>
  18. //定义界面底部区域内容,请注意修改下面广告图片地址
  19. $email_footer = 
  20. <p style=”color: #6e6e6e;font-size:13px;line-height:24px;”>(此邮件由系统自动发出, 请勿回复。)</p>
  21. </div>
  22. <div class=”emailad” style=”margin-top: 24px;”>
  23. <a href=”‘%20<span%20class=”pun”>. get_bloginfo(‘url’<span%20class=”pun”>) . ‘”></span%20class=”pun”></span%20class=”pun”>
  24. <img src=”广告地址” alt=”” style=”margin: auto;width:100%;max-width:700px;height: auto;”>
  25. </a>
  26. </div>
  27. <p style=”color: #6e6e6e;font-size:13px;line-height:24px;text-align:right;padding:0 32px”>邮件来自:<a href=”‘%20<span%20class=”pun”>.get_bloginfo(‘url’<span%20class=”pun”>) . ‘” style=”color:#51a0e3;text-decoration:none”>’%20<span%20class=”pun”>. get_option(“blogname”) . ‘</a></p></span%20class=”pun”></span%20class=”pun”></span%20class=”pun”>
  28. </div>
  29. </div>
  30. </div>
  31. <span%20class=”pun”>;</span%20class=”pun”>
  32. define (’emailfooter’<span%20class=”pun”>, $email_footer );</span%20class=”pun”>
  33. //修改网站默认发信人以及邮箱
  34. function new_from_name($email){
  35. $wp_from_name = get_option(‘blogname’<span%20class=”pun”>);</span%20class=”pun”>
  36. return $wp_from_name;
  37. }
  38. function new_from_email($email) {
  39. $wp_from_email = get_option(‘admin_email’<span%20class=”pun”>);</span%20class=”pun”>
  40. return $wp_from_email;
  41. }
  42. add_filter(‘wp_mail_from_name’<span%20class=”pun”>, ‘new_from_name’<span%20class=”pun”>);</span%20class=”pun”></span%20class=”pun”>
  43. add_filter(‘wp_mail_from’<span%20class=”pun”>, ‘new_from_email’<span%20class=”pun”>);</span%20class=”pun”></span%20class=”pun”>
  44. //评论通过通知评论者
  45. add_action(‘comment_unapproved_to_approved’<span%20class=”pun”>, ‘iwill_comment_approved’<span%20class=”pun”>);</span%20class=”pun”></span%20class=”pun”>
  46. function iwill_comment_approved($comment) {
  47. if(is_email($comment->comment_author_email)) {
  48. $post_link = get_permalink($comment->comment_post_ID);
  49. // 邮件标题,可自行更改
  50. $title = ‘您在 [‘%20<span%20class=”pun”>. get_option(‘blogname’<span%20class=”pun”>) . ‘] 的评论已通过审核’<span%20class=”pun”>;</span%20class=”pun”></span%20class=”pun”></span%20class=”pun”>
  51. // 邮件内容,按需更改。如果不懂改,可以给我留言
  52. $body = emailheadertop.‘留言审核通过通知’<span%20class=”pun”>.emailheaderbot.‘<p style=”color: #6e6e6e;font-size:13px;line-height:24px;”>您在’%20<span%20class=”pun”>. get_option(‘blogname’<span%20class=”pun”>) . ‘《<a href=”‘<span%20class=”pun”>.$post_link.‘”>’<span%20class=”pun”>.get_the_title($comment->comment_post_ID).‘</a>》发表的评论:</p></span%20class=”pun”></span%20class=”pun”></span%20class=”pun”></span%20class=”pun”></span%20class=”pun”>
  53. <p style=”color: #6e6e6e;font-size:13px;line-height:24px;padding:10px 20px;background:#f8f8f8;margin:0px”>’<span%20class=”pun”>.$comment->comment_content.‘</p></span%20class=”pun”>
  54. <p style=”color: #6e6e6e;font-size:13px;line-height:24px;”>已通过管理员审核并显示。<br />
  55. 您可在此查看您的评论:<a href=”‘<span%20class=”pun”>.get_comment_link( $comment->comment_ID ).‘”>前往查看</a></p>’<span%20class=”pun”>.emailfooter;</span%20class=”pun”></span%20class=”pun”>
  56. @wp_mail($comment->comment_author_email, $title, $body, “Content-Type: text/html; charset=UTF-8”);
  57. }
  58. }
  59. /* 邮件评论回复美化版 */
  60. function comment_mail_notify($comment_id) {
  61. $admin_email = get_bloginfo (‘admin_email’<span%20class=”pun”>);</span%20class=”pun”>
  62. $comment = get_comment($comment_id);
  63. $comment_author_email = trim($comment->comment_author_email);
  64. $parent_id = $comment->comment_parent ? $comment->comment_parent : <span%20class=”pun”>;</span%20class=”pun”>
  65. $to = $parent_id ? trim(get_comment($parent_id)->comment_author_email) : <span%20class=”pun”>;</span%20class=”pun”>
  66. $spam_confirmed = $comment->comment_approved;
  67. if (($parent_id != <span%20class=”pun”>) && ($spam_confirmed != ‘spam’<span%20class=”pun”>) && ($to != $admin_email)) {</span%20class=”pun”></span%20class=”pun”>
  68. $wp_email = ‘no-reply@’%20<span%20class=”pun”>. preg_replace(‘#^www\.#’<span%20class=”pun”>, <span%20class=”pun”>, strtolower($_SERVER[‘SERVER_NAME’<span%20class=”pun”>]));</span%20class=”pun”></span%20class=”pun”></span%20class=”pun”></span%20class=”pun”>
  69. $subject = ‘您在 [‘%20<span%20class=”pun”>. get_option(“blogname”) . ‘] 的留言有了新回复’<span%20class=”pun”>;</span%20class=”pun”></span%20class=”pun”>
  70. $message = emailheadertop.‘您在’%20<span%20class=”pun”>. get_option(“blogname”) . ‘上的留言有回复啦!’<span%20class=”pun”>.emailheaderbot.</span%20class=”pun”></span%20class=”pun”>
  71. <p style=”color: #6e6e6e;font-size:13px;line-height:24px;”>’%20<span%20class=”pun”>. trim(get_comment($parent_id)->comment_author) . ‘, 您好!</p></span%20class=”pun”>
  72. <p style=”color: #6e6e6e;font-size:13px;line-height:24px;”>您在《’%20<span%20class=”pun”>. get_the_title($comment->comment_post_ID) .‘》的留言:<br /></span%20class=”pun”>
  73. <p style=”color: #6e6e6e;font-size:13px;line-height:24px;padding:10px 20px;background:#f8f8f8;margin:0px”>’<span%20class=”pun”>.trim(get_comment($parent_id)->comment_content) . ‘</p></span%20class=”pun”>
  74. <p style=”color: #6e6e6e;font-size:13px;line-height:24px;”>’%20<span%20class=”pun”>. trim($comment->comment_author) . ‘ 给你的回复:<br /></span%20class=”pun”>
  75. <p style=”color: #6e6e6e;font-size:13px;line-height:24px;padding:10px 20px;background:#f8f8f8;margin:0px”>’<span%20class=”pun”>.trim($comment->comment_content) . ‘</p></span%20class=”pun”>
  76. <p style=”color: #6e6e6e;font-size:13px;line-height:24px;”>你可以点击<a href=”‘%20<span%20class=”pun”>.htmlspecialchars(get_comment_link($parent_id, array(‘type’%20<span%20class=”pun”>=> ‘comment’<span%20class=”pun”>))) . ‘”>查看完整内容</a></p></span%20class=”pun”></span%20class=”pun”></span%20class=”pun”>
  77. <p style=”color: #6e6e6e;font-size:13px;line-height:24px;”>欢迎再度光临<a href=”‘%20<span%20class=”pun”>. get_option(‘home’<span%20class=”pun”>) .‘”>’%20<span%20class=”pun”>. get_option(‘blogname’<span%20class=”pun”>) . ‘</a></p></span%20class=”pun”></span%20class=”pun”></span%20class=”pun”></span%20class=”pun”>
  78. <span%20class=”pun”>.emailfooter;</span%20class=”pun”>
  79. $from = “From: \”” . get_option(‘blogname’<span%20class=”pun”>) . “\” <$wp_email>”;</span%20class=”pun”>
  80. $headers = “$from\nContent-Type: text/html; charset=” . get_option(‘blog_charset’<span%20class=”pun”>) . “\n”;</span%20class=”pun”>
  81. wp_mail( $to, $subject, $message, $headers );
  82. }
  83. }
  84. add_action(‘comment_post’<span%20class=”pun”>, ‘comment_mail_notify’<span%20class=”pun”>);</span%20class=”pun”></span%20class=”pun”>
  85. // 博客后台登录失败时发送邮件通知管理员
  86. function wp_login_failed_notify(){
  87. date_default_timezone_set(‘PRC’<span%20class=”pun”>);</span%20class=”pun”>
  88. $admin_email = get_bloginfo(‘admin_email’<span%20class=”pun”>);</span%20class=”pun”>
  89. $to = $admin_email;
  90. $subject = ‘【登录失败】有人使用了错误的用户名或密码登录’%20<span%20class=”pun”>. get_bloginfo(‘name’<span%20class=”pun”>) . ‘!’<span%20class=”pun”>;</span%20class=”pun”></span%20class=”pun”></span%20class=”pun”>
  91. $message = emailheadertop . get_bloginfo(‘name’<span%20class=”pun”>) . ‘账户登录失败通知!’<span%20class=”pun”>.emailheaderbot.</span%20class=”pun”></span%20class=”pun”>
  92. <div style=”padding:0;font-weight:bold;color:#6e6e6e;font-size:16px”>尊敬的管理员您好!</div>
  93. <p style=”color: red;font-size:13px;line-height:24px;”>’%20<span%20class=”pun”>. get_bloginfo(‘name’<span%20class=”pun”>) . ‘有一条登录失败的记录产生,若登录操作不是您产生的,请及时注意网站安全!</p></span%20class=”pun”></span%20class=”pun”>
  94. <table cellpadding=”0″ cellspacing=”0″ border=”0″ style=”width:100%;border-top:1px solid #eee;border-left:1px solid #eee;color:#6e6e6e;font-size:16px;font-weight:normal”>
  95. <thead><tr><th colspan=”2″ style=”padding:10px 0;border-right:1px solid #eee;border-bottom:1px solid #eee;text-align:center;background:#f8f8f8;”>失败信息如下</th></tr></thead>
  96. <tbody>
  97. <tr>
  98. <td style=”padding:10px 0;border-right:1px solid #eee;border-bottom:1px solid #eee;text-align:center;width:100px”>登录名</td>
  99. <td style=”padding:10px 20px 10px 30px;border-right:1px solid #eee;border-bottom:1px solid #eee;line-height:30px”>’%20<span%20class=”pun”>. $_POST[‘log’<span%20class=”pun”>] . ‘</td></span%20class=”pun”></span%20class=”pun”>
  100. </tr>
  101. <tr>
  102. <td style=”padding:10px 0;border-right:1px solid #eee;border-bottom:1px solid #eee;text-align:center”>尝试的密码</td>
  103. <td style=”padding:10px 20px 10px 30px;border-right:1px solid #eee;border-bottom:1px solid #eee;line-height:30px”>’%20<span%20class=”pun”>. $_POST[‘pwd’<span%20class=”pun”>] . ‘</td></span%20class=”pun”></span%20class=”pun”>
  104. </tr>
  105. <tr>
  106. <td style=”padding:10px 0;border-right:1px solid #eee;border-bottom:1px solid #eee;text-align:center;”>登录时间</td>
  107. <td style=”padding:10px 20px 10px 30px;border-right:1px solid #eee;border-bottom:1px solid #eee;line-height:30px”>’%20<span%20class=”pun”>. date(“Y-m-d H:i:s”) . ‘</td></span%20class=”pun”>
  108. </tr>
  109. <tr>
  110. <td style=”padding:10px 0;border-right:1px solid #eee;border-bottom:1px solid #eee;text-align:center;”>登录IP</td>
  111. <td style=”padding:10px 20px 10px 30px;border-right:1px solid #eee;border-bottom:1px solid #eee;line-height:30px”>’%20<span%20class=”pun”>. $_SERVER[‘REMOTE_ADDR’<span%20class=”pun”>] . ‘</td></span%20class=”pun”></span%20class=”pun”>
  112. </tr>
  113. </tbody>
  114. </table>
  115. <span%20class=”pun”>.emailfooter;</span%20class=”pun”>
  116. wp_mail( $to, $subject, $message, “Content-Type: text/html; charset=UTF-8” );
  117. }
  118. add_action(‘wp_login_failed’<span%20class=”pun”>, ‘wp_login_failed_notify’<span%20class=”pun”>);</span%20class=”pun”></span%20class=”pun”>
  119. add_filter(‘logout_url’<span%20class=”pun”>, ‘mk_logout_redirect_home’<span%20class=”pun”>, 10, 2);</span%20class=”pun”></span%20class=”pun”>
  120. function mk_logout_redirect_home($logouturl, $redir){
  121. $redir = home_url();
  122. return $logouturl . ‘&redirect_to=’%20<span%20class=”pun”>. urlencode($redir);</span%20class=”pun”>
  123. }
  124. //用户更新账户通知用户
  125. function user_profile_update( $user_id ) {
  126. $site_url = get_bloginfo(‘wpurl’<span%20class=”pun”>);</span%20class=”pun”>
  127. $site_name = get_bloginfo(‘wpname’<span%20class=”pun”>);</span%20class=”pun”>
  128. $user_info = get_userdata( $user_id );
  129. $to = $user_info->user_email;
  130. $subject = “”.$site_name.“账户更新”;
  131. $message = emailheadertop.‘您在’%20<span%20class=”pun”>.$site_name. ‘账户资料修改成功!’<span%20class=”pun”>.emailheaderbot.‘<p style=”color: #6e6e6e;font-size:13px;line-height:24px;”>亲爱的 ‘%20<span%20class=”pun”>.$user_info->display_name . ‘<br/>您的资料修改成功!<br/>谢谢您的光临</p>’<span%20class=”pun”>.emailfooter;</span%20class=”pun”></span%20class=”pun”></span%20class=”pun”></span%20class=”pun”>
  132. wp_mail( $to, $subject, $message, “Content-Type: text/html; charset=UTF-8”);
  133. }
  134. add_action( ‘profile_update’<span%20class=”pun”>, ‘user_profile_update’<span%20class=”pun”>, 10, 2);</span%20class=”pun”></span%20class=”pun”>
  135. //用户账户被删除通知用户
  136. function iwilling_delete_user( $user_id ) {
  137. global $wpdb;
  138. $site_name = get_bloginfo(‘name’<span%20class=”pun”>);</span%20class=”pun”>
  139. $user_obj = get_userdata( $user_id );
  140. $email = $user_obj->user_email;
  141. $subject = “帐号删除提示:”.$site_name.“”;
  142. $message = emailheadertop.‘您在’%20<span%20class=”pun”>.$site_name. ‘的账户已被管理员删除!’<span%20class=”pun”>.emailheaderbot.‘<p style=”color: #6e6e6e;font-size:13px;line-height:24px;”>如果您对本次操作有什么异议,请联系管理员反馈!<br/>我们会在第一时间处理您反馈的问题.</p>’<span%20class=”pun”>.emailfooter;</span%20class=”pun”></span%20class=”pun”></span%20class=”pun”>
  143. wp_mail( $email, $subject, $message, “Content-Type: text/html; charset=UTF-8”);
  144. }
  145. add_action( ‘delete_user’<span%20class=”pun”>, ‘iwilling_delete_user’%20<span%20class=”pun”>);</span%20class=”pun”></span%20class=”pun”>
  146. // WordPress 发布新文章后邮件通知已注册的用户
  147. function newPostNotify($post_ID) {
  148. if( wp_is_post_revision($post_ID) ) return;
  149. global $wpdb;
  150. $site_name = get_bloginfo(‘name’<span%20class=”pun”>);</span%20class=”pun”>
  151. $post_contents = get_post($post_ID)->post_content;
  152. $get_post_info = get_post($post_ID);
  153. if ( $get_post_info->post_status == ‘publish’%20<span%20class=”pun”>&& $_POST[‘original_post_status’<span%20class=”pun”>] != ‘publish’%20<span%20class=”pun”>) {</span%20class=”pun”></span%20class=”pun”></span%20class=”pun”>
  154. // 读数据库,获取所有用户的email
  155. $wp_user_email = $wpdb->get_col(“SELECT DISTINCT user_email FROM $wpdb->users”);
  156. // 邮件标题
  157. $subject = ‘Hi!’<span%20class=”pun”>.$site_name.‘发布新文章啦!’<span%20class=”pun”>;</span%20class=”pun”></span%20class=”pun”>
  158. // 邮件内容
  159. $message = emailheadertop.$site_name. ‘发布新文章啦!’<span%20class=”pun”>.emailheaderbot.</span%20class=”pun”>
  160. <div style=”padding:0;font-weight:bold;color:#6e6e6e;font-size:16px”>文章标题:’%20<span%20class=”pun”>. get_the_title($post_ID) . ‘</div></span%20class=”pun”>
  161. <p style=”color: #6e6e6e;font-size:13px;line-height:24px;”>’%20<span%20class=”pun”>. mb_strimwidth($post_contents, 0, 320,“…”) . ‘</p></span%20class=”pun”>
  162. <p style=”color: #6e6e6e;font-size:13px;line-height:24px;text-align:right”><a href=”‘%20<span%20class=”pun”>. get_permalink($post_ID) .‘”>查看全文</a><br /></p></span%20class=”pun”>
  163. <span%20class=”pun”>.emailfooter;</span%20class=”pun”>
  164. // 发邮件
  165. $message_headers = “Content-Type: text/html; charset=\”utf-8\”\n”;
  166. wp_mail($wp_user_email, $subject, $message, $message_headers);
  167. }
  168. }
  169. add_action(‘publish_post’<span%20class=”pun”>, ‘newPostNotify’<span%20class=”pun”>);</span%20class=”pun”></span%20class=”pun”>
  170. ?>

注意事项:

1、自行设计 emailbg.jpg 文件,放在主题的 images 文件夹下面,请与代码第三行地址保持一致;

2、代码26行定义了一个广告图片,建议大家使用photoshop制作以后换成自己的;

3、174行定义了摘要显示字数,您可以随意换成您自己的,然后去看看效果吧!


IT速极网-【教程资源】 » WordPress优化技巧:评论审核+回复+会员信息更新+新文章邮件通知系统美化.

常见问题FAQ

RD.IT速极网可以技术支持吗?
需要技术技术资源联系客服即可!

发表评论