加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > CMS系统 > 正文

WordPress评论邮件通知无插件实现思路及代码

发布时间:2020-05-24 22:29:48 所属栏目:CMS系统 来源:互联网
导读:通常我们喜欢使用Wordpress Thread Comment 或 Mail To Commenter这两款插件前者久未更新,已测支持WordPress2.9.2版本 其他的未测试,优点,不进垃圾箱,支持嵌套回复,今天来通过无插件实现WordPress评论邮件通知

通常我们喜欢使用Wordpress Thread Comment 或 Mail To Commenter这两款插件前者久未更新,已测支持WordPress2.9.2版本 其他的未测试,优点,不进垃圾箱,支持嵌套回复。 后者据网友反馈 该插件容易造成邮件延迟 邮件进垃圾箱或收不到邮件的情况。 今天来通过无插件实现WordPress评论邮件通知,先看效果:

方法

:在主题目录的functions中增加以下代码: <div class="msgborder" id="phpcode11"> / comment_mail_notify v1.0 by willin kan. (無勾選欄) / function comment_mail_notify($comment_id) { $admin_email = get_bloginfo ('admin_email'); // $admin_email 可改為你指定的 e-mail. $comment = get_comment($comment_id); $comment_author_email = trim($comment-&gt;comment_author_email); $parent_id = $comment-&gt;comment_parent ? $comment-&gt;comment_parent : ''; $to = $parent_id ? trim(get_comment($parent_id)-&gt;comment_author_email) : ''; $spam_confirmed = $comment-&gt;comment_approved; if (($parent_id != '') &amp;&amp; ($spam_confirmed != 'spam') &amp;&amp; ($to != $admin_email) &amp;&amp; ($comment_author_email == $admin_email)) { / 上面的判斷式,決定發出郵件的必要條件: ($parent_id != '') &amp;&amp; ($spam_confirmed != 'spam'): 回覆的,而且不是 spam 才可發,必需!! ($to != $admin_email) : 不發給 admin. ($comment_author_email == $admin_email) : 只有 admin 的回覆才可發. 可視個人需求修改以上條件. / $wp_email = 'no-reply@' . preg_replace('#^www.#','',strtolower($_SERVER['SERVER_NAME'])); // e-mail 發出點,no-reply 可改為可用的 e-mail. $subject = '您在【' . getoption("blogname") . '】的留言有回复了!去看看吧 ^^'; $message = ' &lt;div style="margin: 1em 40px 1em 40px;background-color:#eef2fa;border:1px solid #d8e3e8;color:#111;padding: 0 15px;-moz-border-radius:5px;-webkit-border-radius:5px;-khtml-border-radius:5px;border-radius:5px;"&gt; &lt;p&gt;Hi !&lt;h3&gt;' . trim(get_comment($parent_id)-&gt;comment_author) . '&lt;/h3&gt;,您好! 您曾在&lt;h3&gt;《' . get_the_title($comment-&gt;comment_post_ID) . '》&lt;/h3&gt;的留言有了新回复。&lt;/p&gt;&lt;/div&gt; &lt;div style="margin: 1em 40px 1em 40px;background-color:#eef2fa;border:1px solid #d8e3e8;color:#111;padding: 0 15px;-moz-border-radius:5px;-webkit-border-radius:5px;-khtml-border-radius:5px;border-radius:5px;"&gt;&lt;p&gt;&lt;h3&gt; 您&lt;/h3&gt;说:' . trim(get_comment($parent_id)-&gt;comment_content) . '&lt;/p&gt; &lt;p&gt;&lt;h3&gt;' . trim($comment-&gt;comment_author) . '&lt;/h3&gt;说: ' . trim($comment-&gt;comment_content) . '&lt;/p&gt; &lt;p&gt;您可以点击&lt;a href="' . htmlspecialchars(get_comment_link($parent_id)) . '"&gt;查看完整的回复內容。&lt;/a&gt;&lt;/p&gt; &lt;p&gt;欢迎再度光临 &lt;a href="' . get_option('home') . '"&gt;' . get_option('blogname') . '&lt;/a&gt;&lt;/p&gt; &lt;p&gt;(此邮件由系统自动发送,请勿回复。)&lt;/p&gt;&lt;/div&gt; '; $from = "From: "" . get_option('blogname') . "" &lt;$wp_email&gt;"; $headers = "$fromnContent-Type: text/html; charset=" . get_option('blog_charset') . "n"; wp_mail( $to,$subject,$message,$headers ); echo 'mail to ',$to,' ',$message; // for testing } } add_action('comment_post','comment_mail_notify'); // -- END ----------------------------------------

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读