🚀 サイト構築が難しい?手取り足取りご案内します——「WordPressサイト構築伴走」サービスを詳しく見る →

WordPressにコメントメール返信通知を追加する2つの方法

WordPressコメントメール通知は、Naibaが非常に有用な機能だと考えており、あなたとネットユーザーとのインタラクションを増やすことができます。想像してみてください、もしあなたがこの記事で私に„あなたのチュートリアルに従って操作しましたが、どうしてコメントにまだメール通知がないのですか?“と尋ねた場合、あなたは毎日何度も記事をリフレッシュして私が返信したかどうかを確認したいですか、それとも私があなたにコメントした後にメールで„ねえ、あなたのコメントに返信がありますよ“と知らせてほしいですか。

コード実装方法

WordPressブログでコメント後にコメント投稿者にメールを送信する機能を実現するには、2つの方法があります。1つはコードを追加して実現する方法、もう1つはPluginを使用して実現する方法です。コード機能を使用する場合、基本的には以下のコード、または少し修正されたバージョンが使われます。 /* comment_mail_notify v1.0 by willin kan. (すべての返信にメールを送信) */ ブログのAdmin Dashboardにログインし、„外観“タブの„編集“オプションをクリックしてTheme編集画面に入り、functions.phpファイルの間に以下の関数を追加します。1、全員にメールを送信
/* 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 可改为可用的 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 ----------------------------------------
2、訪問者自身にメール通知を受信するかどうかを選択させるfunctions.phpファイルの間に以下の関数を追加します。この関数はコメントボックスの下部に、返信通知を受信するかどうかのオプションを生成します。
/* 开始*/
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'); /* 自动加勾选栏 */ function add_checkbox() { echo '有人回复时邮件通知我'; } add_action('comment_form', 'add_checkbox');
3、ブログ管理者がどのような状況でメールを送信するかを決定させるfunctions.phpファイルの間に以下の関数を追加します:
/* 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 可改为可用的 e-mail.
    $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 ----------------------------------------
コードに興味がある方は、ここをクリックして申杰ブログを見てみる、比較的詳細に書かれています。ただし、このコードは一部のThemeでは機能しないため、NaibaはWenprise Better EmailsというWordPressコメントメール通知Pluginを使用することをお勧めします。

Plugin版方法(推奨)

WordPress评论邮件通知Wenprise Better Emailsは、WordPressコメント審査通知メール、コメント通知記事著者へのメールを美化し、コメントに返信する際に、コメント著者にメール通知を送信する機能を追加します。NaibaがこのWenprise Better Emails Pluginを推奨する理由は以下の通りです:
  1. コメント時のメール返信通知を実現でき、インストールしてすぐに使用可能で、初心者に適しています;
  2. 中国人製のPluginで、使用習慣に合い、インターフェースも美しい;
  3. 購読解除機能を備えており、メールがスパムと判定される確率を減らせます。
Pluginインストールアドレス:https://wordpress.org/plugins/wenprise-better-emails/

コードもPluginも使えない?

コメントメール通知を使用するには前提条件があり、それはサーバーがメールを送信できることです。実際には、ほとんどのホストは直接メール送信をサポートしていないため、サードパーティのPluginを利用してホストからメールを送信する必要があります。つまり、SMTP方式を使用してメールを送信します。以下の記事を参考にしてください。私が使用したWordPressメールPluginのおすすめさらに、コメント承認済みこのプラグインはNaibaも使用したことがあります。コメントが承認された後、投稿者に承認通知メールを送信するものです。

🚀 チュートリアルを見てもまだ迷っていますか?私が直接手を取ってご案内しましょう

「WordPressサイト構築伴走サービス」——ドメイン名の選択、ホスティングの購入から、Themeのインストール、公開、投稿まで、すべてのステップで私が伴走し、遠回りをせずに目標に直行します。

👉 サイト構築伴走サービスについて詳しく知る
🔒

コメントは閉鎖されました

この記事のコメント機能は閉鎖されています。ご質問がある場合は、他の方法でお問い合わせください。

×
二维码

QRコードをスキャンしてフォロー