2026年5月20日

2026年5月20日

WordPressの通知メールをカスタマイズする方法

はじめに

WordPressはユーザー登録、コメント投稿、パスワードリセットなどのタイミングで自動的にメールを送信します。デフォルトのメール内容をブランドに合わせたり、送信先を変更したい場合のカスタマイズ方法を解説します。

解決手順

ステップ1:新規ユーザー登録通知をカスタマイズする

// functions.php — 新規ユーザー登録時の管理者通知を差し替え
// デフォルト関数を無効化してから独自実装を追加
remove_action('register_new_user',   'wp_send_new_user_notifications');
remove_action('edit_user_created_user', 'wp_send_new_user_notifications');

add_action('register_new_user', function(int $user_id) {
    $user = get_userdata($user_id);
    $subject = sprintf('[%s] 新規ユーザー登録: %s', get_bloginfo('name'), $user->user_login);
    $message = sprintf(
        "新しいユーザーが登録されました。\n\nユーザー名: %s\nメール: %s\n管理画面: %s",
        $user->user_login,
        $user->user_email,
        admin_url('users.php')
    );
    wp_mail(get_option('admin_email'), $subject, $message);
});

ステップ2:コメント通知の件名と内容を変更する

// コメント承認通知の件名を変更
add_filter('comment_moderation_subject', function($subject, $comment_id) {
    $comment = get_comment($comment_id);
    $post    = get_post($comment->comment_post_ID);
    return sprintf('[承認待ち] 「%s」へのコメント', $post->post_title);
}, 10, 2);

// コメント投稿通知の件名を変更
add_filter('comment_notification_subject', function($subject, $comment_id) {
    $comment = get_comment($comment_id);
    $post    = get_post($comment->comment_post_ID);
    return sprintf('[新着コメント] 「%s」', $post->post_title);
}, 10, 2);

// 特定カテゴリのコメント通知を止める
add_filter('comment_notification_recipients', function($emails, $comment_id) {
    $comment = get_comment($comment_id);
    if (has_category('news', $comment->comment_post_ID)) {
        return []; // 通知しない
    }
    return $emails;
}, 10, 2);

ステップ3:パスワードリセット通知をカスタマイズする

// パスワードリセットメールの件名
add_filter('retrieve_password_title', function($title, $login, $user_data) {
    return sprintf('[%s] パスワードのリセット手順', get_bloginfo('name'));
}, 10, 3);

// パスワードリセットメールの本文
add_filter('retrieve_password_message', function($message, $key, $login, $user_data) {
    $reset_url = network_site_url("wp-login.php?action=rp&key={$key}&login=" . rawurlencode($login), 'login');
    return sprintf(
        "%s さん、\n\nパスワードリセットのご依頼を受け付けました。\n\n以下のURLから24時間以内に新しいパスワードを設定してください:\n%s\n\nご自身でご依頼でない場合はこのメールを無視してください。",
        $user_data->display_name,
        $reset_url
    );
}, 10, 4);

ステップ4:通知メールをHTMLフォーマットにする

// 全通知メールをHTMLに切り替える
add_filter('wp_mail_content_type', fn() => 'text/html; charset=UTF-8');

// HTML通知メールの例
add_action('register_new_user', function(int $user_id) {
    $user    = get_userdata($user_id);
    $subject = '【' . get_bloginfo('name') . '】ご登録ありがとうございます';
    $message = sprintf(
        '<p>%s さん、ご登録ありがとうございます。</p><p><a href="%s">サイトへアクセス</a></p>',
        esc_html($user->display_name),
        esc_url(home_url())
    );
    wp_mail($user->user_email, $subject, $message, 'Content-Type: text/html; charset=UTF-8');
});

注意事項

  • wp_mail_content_type フィルタでHTMLに切り替えた場合、すべての送信メールがHTMLになります。必要なら送信後 remove_filter で元に戻してください
  • comment_notification_recipients フィルタで空配列を返すと通知が送信されません
  • retrieve_password_message の第4引数を受け取るには add_filter の第4引数に 10, 4 を指定する必要があります

まとめ

WordPressの通知メールは各フィルタフック(comment_moderation_subjectretrieve_password_titleretrieve_password_message など)で件名・本文・送信先を個別にカスタマイズできます。ブランドに合わせたメールに変更することでユーザー体験が向上します。

お気軽にご相談ください

お見積りへ お問い合わせへ