三款非插件WordPress回复自动发邮件代码

博客搬家并启用新主题,今天就启用回复自动发邮件功能,从Forece那里找到三款代码,省去用插件的麻烦,三款的功能基本相同,但稍有不同:一个是用户自己决定收不收邮件,一个是管理员决定,一个是都发,根据自己需要选择吧。

这个是 Ajax comments 评论回应邮件通知的升级版,主要改进了以下功能。

1. 所有的模板都适用,不只是 Ajax comments.

2. 管理者在后台回复也可以发邮件.

如果大家对以前的版本感兴趣,可以通过传送门过去。。。。

安裝步驟:

1. 如果你用了上一版的 “Ajax comments 回应邮件通知”, 请先在 comments-ajax.php 删除上一版的代码.

2. 在下面的三种方式,选择你想用的代码, copy 到 functions.php 文件里.

必须注意的是:你的服务器一定要支持 mail() 功能。测试方法:在登陆页面故意点击“忘记密码”,收到重置密码邮件就说明主机支持 mail() 功能,没收到就可以下课了。

一、有勾选项,由访客决定是否要接收邮件通知:

(会在模板自动加勾选项,如果不想自动加,可把最后一小段删除)

/* comment_mail_notify v1.0 by willin kan. (有勾选项,由游客决定) */
function comment_mail_notify($comment_id) {
  $admin_notify = '1'; // admin 要不要回复通知 ( '1'=要 ; '0'=不要 )
  $admin_email = get_bloginfo ('admin_email'); // $admin_email 可改为你指定的 e-mail.
  $comment = get_comment($comment_id);
  $comment_author_email = trim($comment->comment_author_email);
  $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
  global $wpdb;
  if ($wpdb->query("Describe {$wpdb->comments} comment_mail_notify") == '')
    $wpdb->query("ALTER TABLE {$wpdb->comments} ADD COLUMN comment_mail_notify TINYINT NOT NULL DEFAULT 0;");
  if (($comment_author_email != $admin_email && isset($_POST['comment_mail_notify'])) || ($comment_author_email == $admin_email && $admin_notify == '1'))
    $wpdb->query("UPDATE {$wpdb->comments} SET comment_mail_notify='1' WHERE comment_ID='$comment_id'");
  $notify = $parent_id ? get_comment($parent_id)->comment_mail_notify : '0';
  $spam_confirmed = $comment->comment_approved;
  if ($parent_id != '' && $spam_confirmed != 'spam' && $notify == '1') {
    $wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 发出点, no-reply可改为可用的 e-mail.
    $to = trim(get_comment($parent_id)->comment_author_email);
    $subject = '您在 [' . get_option("blogname") . '] 的留言有了回应';
    $message = '
    

' . trim(get_comment($parent_id)->comment_author) . ', 您好!

您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:
' . trim(get_comment($parent_id)->comment_content) . '

' . trim($comment->comment_author) . ' 给您的回应:
' . trim($comment->comment_content) . '

您可以点击 查看完整内容并进行反击!

欢迎再次光临 ' . get_option('blogname') . '

(此邮件由系统自动发出,请勿回复.)

'; $from = "From: \"" . get_option('blogname') . "\" <$wp_email>"; $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n"; wp_mail( $to, $subject, $message, $headers ); //echo 'mail to ', $to, '
' , $subject, $message; // for testing } } add_action('comment_post', 'comment_mail_notify'); // -- END ---------------------------------------- /* 自动加勾选 */ function add_checkbox() { echo ''; } add_action('comment_form', 'add_checkbox'); // -- END ----------------------------------------

二、无勾选项,由管理者决定在什么条件下发邮件:

/* 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->comment_author_email);
  $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
  $to = $parent_id ? trim(get_comment($parent_id)->comment_author_email) : '';
  $spam_confirmed = $comment->comment_approved;
  if (($parent_id != '') && ($spam_confirmed != 'spam') && ($to != $admin_email) && ($comment_author_email == $admin_email)) {
    /* 上面的判断决定发出邮件的必要条件:
    ($parent_id != '') && ($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 可改为可用的email。
    $subject = '您在 [' . get_option("blogname") . '] 的留言有了回应。';
    $message = '
    

' . trim(get_comment($parent_id)->comment_author) . ', 您好!

您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:
' . trim(get_comment($parent_id)->comment_content) . '

' . trim($comment->comment_author) . ' 给您的回应:
' . trim($comment->comment_content) . '

您可以点击 查看回应完整内容

欢迎再次光临 ' . get_option('blogname') . '

(此邮件由系统自动发出,请勿回复。)

'; $from = "From: \"" . get_option('blogname') . "\" <$wp_email>"; $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n"; wp_mail( $to, $subject, $message, $headers ); //echo 'mail to ', $to, '
' , $subject, $message; // for testing } } add_action('comment_post', 'comment_mail_notify'); // -- END ----------------------------------------

三、所有回复都发邮件:

/* comment_mail_notify v1.0 by willin kan. (所有回复都发邮件) */
function comment_mail_notify($comment_id) {
  $comment = get_comment($comment_id);
  $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
  $spam_confirmed = $comment->comment_approved;
  if (($parent_id != '') && ($spam_confirmed != 'spam')) {
    $wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); //e-mail 发出点, no-reply 可改为可用的email。
    $to = trim(get_comment($parent_id)->comment_author_email);
    $subject = '您在 [' . get_option("blogname") . '] 的留言有了回应';
    $message = '
    

' . trim(get_comment($parent_id)->comment_author) . ', 您好!

您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:
' . trim(get_comment($parent_id)->comment_content) . '

' . trim($comment->comment_author) . ' 给您的回应:
' . trim($comment->comment_content) . '

您可以点击 查看回应内容

欢迎再次光临 ' . get_option('blogname') . '

(此邮件由系统自动发出,请勿回复。)

'; $from = "From: \"" . get_option('blogname') . "\" <$wp_email>"; $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n"; wp_mail( $to, $subject, $message, $headers ); //echo 'mail to ', $to, '
' , $subject, $message; // for testing } } add_action('comment_post', 'comment_mail_notify'); // -- END ----------------------------------------

◎2010/4/2 发现这个代码在评论分页的 get_comment_link() 有个 bug.

这 bug 对使用 comments 和 trackbacks/pingbacks 分离的情况才会出现,没分离的没有影响.

当直接调用 get_comment_link() 因为没经过 wp_list_comments(‘type=comment’) 函数,它会以所有的评论作为分页对象. 所以 trackbacks/pingbacks 数量多的时候会让 cpage 多算了, 本来是 cpage=7 会成了 cpage=8, 所以点击邮件里的“查看回应完整内容会找不到正确评论页面”.

— 修正方式 —-

将以上的:

get_comment_link($parent_id)

改为:

get_comment_link($parent_id, array('type' => 'comment'))

加入的参数是让它选取 comment 数量计算就好.

评论样式有使用 comments 和 trackbacks/pingbacks 分离的童鞋,请进行修改,没用到的就不用改了。

思章老师

认准了方向,就要勇敢地走下去,十年磨一剑,我相信,只要坚持,一切都有可能。

相关日志

  1. 没有图片

    2009.08.21

    国内最强内容管理系统 DedeCMS 5.5 正式版发布

    DedeCMS 5.5 正式版于8月14日 …

  2. 没有图片

    2012.11.22

    服务器端口被占用解决方案

    在服务器上安装某服务时,可能会遇到端口被占用…

  3. 没有图片

    2011.05.18

    10个实用的 WordPress .htaccess技巧

    文章转自水脉烟香,非常有价值,转发过来与更多…

  4. 没有图片

    2013.08.28

    WordPress安装教程

    玩WordPress多年了,但对于入门级的W…

  5. 没有图片

    2011.11.25

    感恩节礼物:老鹰主机黑色星期五优惠码

    一直在期待着老鹰主机的黑色星期五优惠码,今天…

  6. 没有图片

    2012.06.20

    IE6/IE7/IE8/Firefox浏览器兼容CSS HACK代码+示例

    菠萝对兼容性绝对是个外行,今天才开始正式学习…

评论

  1. دولقۇن731 2011.06.30 2:31上午

    改天分享这个(维吾尔语言的), 谢谢。

  2. 老衲 2011.04.27 8:46下午

    Ajax comments里面删除什么?
    弄了很长时间弄不好评论邮件….

  3. 黑豆 2011.03.25 9:07下午

    香港主机好慢

    • 落花生 2011.03.26 8:49上午

      香港主机比国内肯定要慢一些,但比美国应该能快一些。