有评论邮件通知博主代码

你的服務器一定要有 mail() 功能. 測試方式: 在登入頁故意按下 '忘記密碼', 收到郵件就有 mail() 功能; 沒收到郵件的可以下課了. 請先安裝好 smtp 插件, 確定服務器可發信了再回來吧~

有勾選欄, 由訪客決定是否要回應郵件通知:

(會在模板自動加勾選欄, 如果不想自動加, 可把後面一小段刪除.)

 

/* comment_mail_notify v1.0 by willin kan. (有勾選欄, 由訪客決定) */

function comment_mail_notify($comment_id) {

  $admin_notify = '1'; // admin 要不要收回覆通知 ( '1'=要 ; '0'=不要 )

  $admin_email = get_bloginfo ('i@ashi.im'); // $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 = '

<div style="background-color:#eef2fa; border:1px solid #d8e3e8; color:#111; padding:0 15px; -moz-border-radius:5px; -webkit-border-radius:5px; -khtml-border-radius:5px;">

<p><strong>' . trim(get_comment($parent_id)->comment_author) . '</strong>, 您好!</p>

<p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:

' . trim(get_comment($parent_id)->comment_content) . '</p>

<p><strong>' . $comment_author . '</strong> 给您的回复:

' . trim($comment->comment_content) . '<br /></p>

<p>您可以点击 <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '"> 查看回应完整內容</a></p>

<p style="float:right"> —— By <a href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>

</div>';

    $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, '<br/> ' , $subject, $message; // for testing

  }

}

add_action('comment_post', 'comment_mail_notify');


/* 自動加勾選欄 */

function add_checkbox() {

  echo '<input type="checkbox" name="comment_mail_notify" id="comment_mail_notify" value="comment_mail_notify" checked="checked" style="margin-left:20px;" /><label for="comment_mail_notify">有人回复时邮件通知我</label>';

}

add_action('comment_form', 'add_checkbox');


// — END —————————————-

評論式樣有使用 comments 和 trackbacks/pingbacks 分離的童鞋, 請進行修改. 沒用到的就不能改.

发表评论?

1 条评论。

  1. 博主太有才了!我学习了

发表评论