三款非插件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. 没有图片

    2012.06.25

    Discuz!x2防CC攻击设置详细教程

    在config_global.php文件中有…

  2. 2013.12.20

    Office 2013 激活教程(自用|临时分享)

    本篇文章纯是为了记录自己的 Office 2…

  3. 没有图片

    2010.05.12

    WordPress评论统计的一大Bug

    WordPress目前来看已经很完善了,但菠…

  4. 没有图片

    2009.07.05

    WordPress 2.8 兼容的风格列表

    随着Wordpress 2.8版本的发布,很…

  5. 没有图片

    2010.02.19

    最全的分页CSS样式代码

    为了给一公司设计网站,整理了一下分页CSS代…

  6. 没有图片

    2009.07.06

    WordPress 首页或列表页只显示摘要而不是全文的方法

    Wordpress系统默认的首页是显示文章的…

评论

  1. Louis Han 2011.03.24 10:07下午

    不太喜欢折腾 还是用插件算了

    • 落花生 2011.03.24 10:31下午

      我也是败了好几次才成功的。

  2. 学夫子 2011.03.24 9:38下午

    以前用wp的时候觉得这个好麻烦,搞了半天没有搞定,最后用emlog,一个插件就搞定了……而且是同样的主机

    • 落花生 2011.03.24 10:30下午

      emlog就是非常易用,这点做得不错。

  3. 德州SEO 2011.03.24 9:28下午

    哦学习了