【WordPress入門】WordPress で管理者やユーザーに送られるメールをカスタマイズする

WordPress ではユーザーの新規登録やパスワードリセットなどの様々な状況で、ユーザーにメールが送られます。ここでは WordPress でどのような状況でメールが送信されるか、またその際にどのようなフィルターフックが利用できるかをまとめました。

目次

WordPress でメールが送信されるタイミング

WordPress でメールが送信されるのは wp_mail 関数が実行されるときです。WordPress のコアファイル内で wp_mail が実行されているのは以下のファイルになります。

  • wp-admin/includes/class-wp-automatic-updater.php
  • wp-admin/includes/privacy-tools.php
  • wp-admin/includes/upgrade.php
  • wp-admin/includes/misc.php
  • wp-admin/ms-delete-site.php
  • wp-admin/user-new.php
  • wp-includes/functions.php
  • wp-includes/ms-functions.php
  • wp-includes/user.php
  • wp-includes/class-wp-recovery-mode-email-service.php
  • wp-includes/pluggable.php

この中で、

  • wp-admin/includes/class-wp-automatic-updater.php (コアやテーマ、プラグインの自動更新時に送られる)
  • wp-admin/includes/upgrade.php (WordPress がインストールされると送られる)
  • wp-admin/includes/misc.php (サイト管理者のメールアドレスを変更しようとしたときに送られる)
  • wp-includes/functions.php (サイト管理者のメールアドレスの変更が完了したときに送られる)
  • wp-includes/class-wp-recovery-mode-email-service.php (サイトで何か問題が発生した際に送られる)

についてはサイト管理者だけが受け取るメールであるため、多くの場合カスタマイズすることはないと思います。また

  • wp-admin/ms-delete-site.php (マルチサイトネットワークでサイト削除のリクエストがされたときに送られる)
  • wp-admin/user-new.php (マルチサイトでサイトに招待されたときに送られる)
  • wp-includes/ms-functions.php

はマルチサイトに関連するファイルであり、使われる頻度が少ないためここではこれらのファイルについてはスキップして、

  • wp-admin/includes/privacy-tools.php
  • wp-includes/user.php
  • wp-includes/pluggable.php

について見ていきます。

パスワードが変更されたときにユーザーに送られるメール

まずは管理画面でユーザーパスワードが変更された際に送られるメールのカスタマイズですが、これは wp-includes/user.php で定義されている wp_udpate_user 関数内にある password_change_email フィルターを用います。

/**
 * Filters the contents of the email sent when the user's password is changed.
 *
 * @since 4.3.0
 *
 * @param array $pass_change_email {
 *     Used to build wp_mail().
 *
 *     @type string $to      The intended recipients. Add emails in a comma separated string.
 *     @type string $subject The subject of the email.
 *     @type string $message The content of the email.
 *         The following strings have a special meaning and will get replaced dynamically:
 *         - ###USERNAME###    The current user's username.
 *         - ###ADMIN_EMAIL### The admin email in case this was unexpected.
 *         - ###EMAIL###       The user's email address.
 *         - ###SITENAME###    The name of the site.
 *         - ###SITEURL###     The URL to the site.
 *     @type string $headers Headers. Add headers in a newline (\r\n) separated string.
 * }
 * @param array $user     The original user array.
 * @param array $userdata The updated user array.
 */
$pass_change_email = apply_filters( 'password_change_email', $pass_change_email, $user, $userdata );

第一引数で渡される配列 $pass_change_email には、メールの送信先や件名、本文、ヘッダーが入っていますので、この配列を変更して return することでカスタマイズすることができます。またメールの本文ですが、

  • ###USERNAME###
  • ###ADMIN_EMAIL###
  • ###EMAIL###
  • ###SITENAME###
  • ###SITEURL###

という文字列はフィルター後の処理でユーザー名、管理者メールアドレス、ユーザーメールアドレス、サイト名、サイトURLに置き換わるようになっています。

メールアドレスが変更されたときにユーザーに送られるメール

パスワード変更時のメールと同じく、wp_update_user 関数内の email_change_email フィルターフックを用いると、ユーザーのメールアドレスが変更されたときに送られるメールをカスタマイズできます。

/**
 * Filters the contents of the email sent when the user's email is changed.
 *
 * @since 4.3.0
 *
 * @param array $email_change_email {
 *     Used to build wp_mail().
 *
 *     @type string $to      The intended recipients.
 *     @type string $subject The subject of the email.
 *     @type string $message The content of the email.
 *         The following strings have a special meaning and will get replaced dynamically:
 *         - ###USERNAME###    The current user's username.
 *         - ###ADMIN_EMAIL### The admin email in case this was unexpected.
 *         - ###NEW_EMAIL###   The new email address.
 *         - ###EMAIL###       The old email address.
 *         - ###SITENAME###    The name of the site.
 *         - ###SITEURL###     The URL to the site.
 *     @type string $headers Headers.
 * }
 * @param array $user     The original user array.
 * @param array $userdata The updated user array.
 */
$email_change_email = apply_filters( 'email_change_email', $email_change_email, $user, $userdata );

メール本文では以下の文字列がメール送信時にしかるべき値に置き換わります。

  • ###USERNAME###
  • ###ADMIN_EMAIL###
  • ###NEW_EMAIL###
  • ###EMAIL###
  • ###SITENAME###
  • ###SITEURL###

パスワードリセットが要求されたときにユーザーに送られるメール

パスワードリセットが要求されたときに送られるメールのカスタマイズには、

  • retrieve_password_title (メール件名を変更できる)
  • retrieve_password_message (メール本文を変更する)
  • retrieve_password_notification_email (メールの送信先、件名、本文、ヘッダーを変更できる)

の3つのフィルターが用意されていますが、v6.0.0 で追加された retrieve_password_notification_email フィルターを利用するとメールの送信先、件名、本文、ヘッダーすべてをカスタマイズすることができますので、これを利用するだけで問題ありません。

/**
 * Filters the subject of the password reset email.
 *
 * @since 2.8.0
 * @since 4.4.0 Added the `$user_login` and `$user_data` parameters.
 *
 * @param string  $title      Email subject.
 * @param string  $user_login The username for the user.
 * @param WP_User $user_data  WP_User object.
 */
$title = apply_filters( 'retrieve_password_title', $title, $user_login, $user_data );

/**
 * Filters the message body of the password reset mail.
 *
 * If the filtered message is empty, the password reset email will not be sent.
 *
 * @since 2.8.0
 * @since 4.1.0 Added `$user_login` and `$user_data` parameters.
 *
 * @param string  $message    Email message.
 * @param string  $key        The activation key.
 * @param string  $user_login The username for the user.
 * @param WP_User $user_data  WP_User object.
 */
$message = apply_filters( 'retrieve_password_message', $message, $key, $user_login, $user_data );

/**
 * Filters the contents of the reset password notification email sent to the user.
 *
 * @since 6.0.0
 *
 * @param array $defaults {
 *     The default notification email arguments. Used to build wp_mail().
 *
 *     @type string $to      The intended recipient - user email address.
 *     @type string $subject The subject of the email.
 *     @type string $message The body of the email.
 *     @type string $headers The headers of the email.
 * }
 * @type string  $key        The activation key.
 * @type string  $user_login The username for the user.
 * @type WP_User $user_data  WP_User object.
 */
$notification_email = apply_filters( 'retrieve_password_notification_email', $defaults, $key, $user_login, $user_data );

プロフィールページからメールアドレスの変更をリクエストしたときにユーザーに送られるメール

ユーザーがプロフィールページからメールアドレスを変更しようとした際に送られるメールのカスタマイズには、 wp-includes/user.php 内の send_confirmation_on_profile_email 関数に用意されている new_user_email_content フィルターフックを使います。ここで送られるメールに記載されているリンクを踏むことで、メールアドレスの変更が完了します。

/**
 * Filters the text of the email sent when a change of user email address is attempted.
 *
 * The following strings have a special meaning and will get replaced dynamically:
 * - ###USERNAME###  The current user's username.
 * - ###ADMIN_URL### The link to click on to confirm the email change.
 * - ###EMAIL###     The new email.
 * - ###SITENAME###  The name of the site.
 * - ###SITEURL###   The URL to the site.
 *
 * @since MU (3.0.0)
 * @since 4.9.0 This filter is no longer Multisite specific.
 *
 * @param string $email_text     Text in the email.
 * @param array  $new_user_email {
 *     Data relating to the new user email address.
 *
 *     @type string $hash     The secure hash used in the confirmation link URL.
 *     @type string $newemail The proposed new email address.
 * }
 */
$content = apply_filters( 'new_user_email_content', $email_text, $new_user_email );

メール本文では以下の文字列がメール送信時にしかるべき値に置き換わります。

  • ###USERNAME###
  • ###ADMIN_URL###
  • ###EMAIL###
  • ###SITENAME###
  • ###SITEURL###

個人データのエクスポートがリクエストされたときにサイト管理者に送られるメール

wp-includes/user.php_wp_privacy_send_request_confirmation_notification 関数では、個人データのエクスポートがリクエストされたときにサイト管理者にメールを送る処理が行われています。ここでは

  • user_request_confirmed_email_to
  • user_request_confirmed_email_subject
  • user_request_confirmed_email_content
  • user_request_confirmed_email_headers

といったフィルターフックが用意されています。

/**
 * Filters the recipient of the data request confirmation notification.
 *
 * In a Multisite environment, this will default to the email address of the
 * network admin because, by default, single site admins do not have the
 * capabilities required to process requests. Some networks may wish to
 * delegate those capabilities to a single-site admin, or a dedicated person
 * responsible for managing privacy requests.
 *
 * @since 4.9.6
 *
 * @param string          $admin_email The email address of the notification recipient.
 * @param WP_User_Request $request     The request that is initiating the notification.
 */
$admin_email = apply_filters( 'user_request_confirmed_email_to', get_site_option( 'admin_email' ), $request );

/**
 * Filters the subject of the user request confirmation email.
 *
 * @since 4.9.8
 *
 * @param string $subject    The email subject.
 * @param string $sitename   The name of the site.
 * @param array  $email_data {
 *     Data relating to the account action email.
 *
 *     @type WP_User_Request $request     User request object.
 *     @type string          $user_email  The email address confirming a request
 *     @type string          $description Description of the action being performed so the user knows what the email is for.
 *     @type string          $manage_url  The link to click manage privacy requests of this type.
 *     @type string          $sitename    The site name sending the mail.
 *     @type string          $siteurl     The site URL sending the mail.
 *     @type string          $admin_email The administrator email receiving the mail.
 * }
 */
$subject = apply_filters( 'user_request_confirmed_email_subject', $subject, $email_data['sitename'], $email_data );

/**
 * Filters the body of the user request confirmation email.
 *
 * The email is sent to an administrator when a user request is confirmed.
 * The following strings have a special meaning and will get replaced dynamically:
 *
 * ###SITENAME###    The name of the site.
 * ###USER_EMAIL###  The user email for the request.
 * ###DESCRIPTION### Description of the action being performed so the user knows what the email is for.
 * ###MANAGE_URL###  The URL to manage requests.
 * ###SITEURL###     The URL to the site.
 *
 * @since 5.8.0
 *
 * @param string $content    The email content.
 * @param array  $email_data {
 *     Data relating to the account action email.
 *
 *     @type WP_User_Request $request     User request object.
 *     @type string          $user_email  The email address confirming a request
 *     @type string          $description Description of the action being performed so the user knows what the email is for.
 *     @type string          $manage_url  The link to click manage privacy requests of this type.
 *     @type string          $sitename    The site name sending the mail.
 *     @type string          $siteurl     The site URL sending the mail.
 *     @type string          $admin_email The administrator email receiving the mail.
 * }
 */
$content = apply_filters( 'user_request_confirmed_email_content', $content, $email_data );

/**
 * Filters the headers of the user request confirmation email.
 *
 * @since 5.4.0
 *
 * @param string|array $headers    The email headers.
 * @param string       $subject    The email subject.
 * @param string       $content    The email content.
 * @param int          $request_id The request ID.
 * @param array        $email_data {
 *     Data relating to the account action email.
 *
 *     @type WP_User_Request $request     User request object.
 *     @type string          $user_email  The email address confirming a request
 *     @type string          $description Description of the action being performed so the user knows what the email is for.
 *     @type string          $manage_url  The link to click manage privacy requests of this type.
 *     @type string          $sitename    The site name sending the mail.
 *     @type string          $siteurl     The site URL sending the mail.
 *     @type string          $admin_email The administrator email receiving the mail.
 * }
 */
$headers = apply_filters( 'user_request_confirmed_email_headers', $headers, $subject, $content, $request_id, $email_data );

メール本文では以下の文字列がメール送信時にしかるべき値に置き換わります。

  • ###USERNAME###
  • ###USER_EMAIL###
  • ###DESCRIPTION###
  • ###MANAGE_URL###
  • ###SITEURL###

個人情報の削除リクエストに対して削除が完了した際にユーザーに送られるメール

wp-includes/user.php_wp_privacy_send_erasure_fulfillment_notification 関数では、個人情報の削除リクエストに対して削除が完了した際にメールが送られます。このときにメールをカスタマイズするために

  • user_erasure_fulfillment_email_to
  • user_erasure_fulfillment_email_subject
  • user_erasure_fulfillment_email_content
  • user_erasure_fulfillment_email_headers

の4つのフィルターが用意されています。

/**
 * Filters the recipient of the data erasure fulfillment notification.
 *
 * @since 4.9.6
 *
 * @param string          $user_email The email address of the notification recipient.
 * @param WP_User_Request $request    The request that is initiating the notification.
 */
$user_email = apply_filters( 'user_erasure_fulfillment_email_to', $request->email, $request );

/**
 * Filters the subject of the email sent when an erasure request is completed.
 *
 * @since 5.8.0
 *
 * @param string $subject    The email subject.
 * @param string $sitename   The name of the site.
 * @param array  $email_data {
 *     Data relating to the account action email.
 *
 *     @type WP_User_Request $request            User request object.
 *     @type string          $message_recipient  The address that the email will be sent to. Defaults
 *                                               to the value of `$request->email`, but can be changed
 *                                               by the `user_erasure_fulfillment_email_to` filter.
 *     @type string          $privacy_policy_url Privacy policy URL.
 *     @type string          $sitename           The site name sending the mail.
 *     @type string          $siteurl            The site URL sending the mail.
 * }
 */
$subject = apply_filters( 'user_erasure_fulfillment_email_subject', $subject, $email_data['sitename'], $email_data );

/**
 * Filters the body of the data erasure fulfillment notification.
 *
 * The email is sent to a user when their data erasure request is fulfilled
 * by an administrator.
 *
 * The following strings have a special meaning and will get replaced dynamically:
 *
 * ###SITENAME###           The name of the site.
 * ###PRIVACY_POLICY_URL### Privacy policy page URL.
 * ###SITEURL###            The URL to the site.
 *
 * @since 5.8.0
 *
 * @param string $content The email content.
 * @param array  $email_data {
 *     Data relating to the account action email.
 *
 *     @type WP_User_Request $request            User request object.
 *     @type string          $message_recipient  The address that the email will be sent to. Defaults
 *                                               to the value of `$request->email`, but can be changed
 *                                               by the `user_erasure_fulfillment_email_to` filter.
 *     @type string          $privacy_policy_url Privacy policy URL.
 *     @type string          $sitename           The site name sending the mail.
 *     @type string          $siteurl            The site URL sending the mail.
 * }
 */
$content = apply_filters( 'user_erasure_fulfillment_email_content', $content, $email_data );

/**
 * Filters the headers of the data erasure fulfillment notification.
 *
 * @since 5.8.0
 *
 * @param string|array $headers    The email headers.
 * @param string       $subject    The email subject.
 * @param string       $content    The email content.
 * @param int          $request_id The request ID.
 * @param array        $email_data {
 *     Data relating to the account action email.
 *
 *     @type WP_User_Request $request            User request object.
 *     @type string          $message_recipient  The address that the email will be sent to. Defaults
 *                                               to the value of `$request->email`, but can be changed
 *                                               by the `user_erasure_fulfillment_email_to` filter.
 *     @type string          $privacy_policy_url Privacy policy URL.
 *     @type string          $sitename           The site name sending the mail.
 *     @type string          $siteurl            The site URL sending the mail.
 * }
 */
$headers = apply_filters( 'user_erasure_fulfillment_email_headers', $headers, $subject, $content, $request_id, $email_data );

メール本文では以下の文字列がメール送信時にしかるべき値に置き換わります。

  • ###SITENAME###
  • ###PRIVACY_POLICY_URL###
  • ###SITEURL###

個人データのエクスポートや削除のリクエストが行われたときにユーザーに送られるメール

個人データのエクスポートや削除リクエストが行われたときに送られるメールをカスタマイズするためのフィルターフックは以下の3つです。

  • user_request_action_email_subject
  • user_request_action_email_content
  • user_request_action_email_headers
/**
 * Filters the subject of the email sent when an account action is attempted.
 *
 * @since 4.9.6
 *
 * @param string $subject    The email subject.
 * @param string $sitename   The name of the site.
 * @param array  $email_data {
 *     Data relating to the account action email.
 *
 *     @type WP_User_Request $request     User request object.
 *     @type string          $email       The email address this is being sent to.
 *     @type string          $description Description of the action being performed so the user knows what the email is for.
 *     @type string          $confirm_url The link to click on to confirm the account action.
 *     @type string          $sitename    The site name sending the mail.
 *     @type string          $siteurl     The site URL sending the mail.
 * }
 */
$subject = apply_filters( 'user_request_action_email_subject', $subject, $email_data['sitename'], $email_data );

/**
 * Filters the text of the email sent when an account action is attempted.
 *
 * The following strings have a special meaning and will get replaced dynamically:
 *
 * ###DESCRIPTION### Description of the action being performed so the user knows what the email is for.
 * ###CONFIRM_URL### The link to click on to confirm the account action.
 * ###SITENAME###    The name of the site.
 * ###SITEURL###     The URL to the site.
 *
 * @since 4.9.6
 *
 * @param string $content Text in the email.
 * @param array  $email_data {
 *     Data relating to the account action email.
 *
 *     @type WP_User_Request $request     User request object.
 *     @type string          $email       The email address this is being sent to.
 *     @type string          $description Description of the action being performed so the user knows what the email is for.
 *     @type string          $confirm_url The link to click on to confirm the account action.
 *     @type string          $sitename    The site name sending the mail.
 *     @type string          $siteurl     The site URL sending the mail.
 * }
 */
$content = apply_filters( 'user_request_action_email_content', $content, $email_data );

/**
 * Filters the headers of the email sent when an account action is attempted.
 *
 * @since 5.4.0
 *
 * @param string|array $headers    The email headers.
 * @param string       $subject    The email subject.
 * @param string       $content    The email content.
 * @param int          $request_id The request ID.
 * @param array        $email_data {
 *     Data relating to the account action email.
 *
 *     @type WP_User_Request $request     User request object.
 *     @type string          $email       The email address this is being sent to.
 *     @type string          $description Description of the action being performed so the user knows what the email is for.
 *     @type string          $confirm_url The link to click on to confirm the account action.
 *     @type string          $sitename    The site name sending the mail.
 *     @type string          $siteurl     The site URL sending the mail.
 * }
 */
$headers = apply_filters( 'user_request_action_email_headers', $headers, $subject, $content, $request_id, $email_data );

メール本文では以下の文字列がメール送信時にしかるべき値に置き換わります。

  • ###DESCRIPTION###
  • ###CONFIRM_URL###
  • ###SITENAME###
  • ###SITEURL###

個人データのエクスポートリクエストが完了したときにデータダウンロードリンクとともにユーザーに送られるメール

wp-admin/includes/privacy-tools.phpwp_privacy_send_personal_data_export_email 関数では、個人データのエクスポートリクエストが完了した際に、エクスポートしたいデータのダウンロードリンクをメールで送っています。この際に送られるメールをカスタマイズするには以下の4つのフィルターフックを使います。

  • wp_privacy_personal_data_email_to
  • wp_privacy_personal_data_email_subject
  • wp_privacy_personal_data_email_content
  • wp_privacy_personal_data_email_headers
/**
 * Filters the recipient of the personal data export email notification.
 * Should be used with great caution to avoid sending the data export link to wrong emails.
 *
 * @since 5.3.0
 *
 * @param string          $request_email The email address of the notification recipient.
 * @param WP_User_Request $request       The request that is initiating the notification.
 */
$request_email = apply_filters( 'wp_privacy_personal_data_email_to', $request->email, $request );

/**
 * Filters the subject of the email sent when an export request is completed.
 *
 * @since 5.3.0
 *
 * @param string $subject    The email subject.
 * @param string $sitename   The name of the site.
 * @param array  $email_data {
 *     Data relating to the account action email.
 *
 *     @type WP_User_Request $request           User request object.
 *     @type int             $expiration        The time in seconds until the export file expires.
 *     @type string          $expiration_date   The localized date and time when the export file expires.
 *     @type string          $message_recipient The address that the email will be sent to. Defaults
 *                                              to the value of `$request->email`, but can be changed
 *                                              by the `wp_privacy_personal_data_email_to` filter.
 *     @type string          $export_file_url   The export file URL.
 *     @type string          $sitename          The site name sending the mail.
 *     @type string          $siteurl           The site URL sending the mail.
 * }
 */
$subject = apply_filters( 'wp_privacy_personal_data_email_subject', $subject, $site_name, $email_data );

/**
 * Filters the text of the email sent with a personal data export file.
 *
 * The following strings have a special meaning and will get replaced dynamically:
 * ###EXPIRATION###         The date when the URL will be automatically deleted.
 * ###LINK###               URL of the personal data export file for the user.
 * ###SITENAME###           The name of the site.
 * ###SITEURL###            The URL to the site.
 *
 * @since 4.9.6
 * @since 5.3.0 Introduced the `$email_data` array.
 *
 * @param string $email_text Text in the email.
 * @param int    $request_id The request ID for this personal data export.
 * @param array  $email_data {
 *     Data relating to the account action email.
 *
 *     @type WP_User_Request $request           User request object.
 *     @type int             $expiration        The time in seconds until the export file expires.
 *     @type string          $expiration_date   The localized date and time when the export file expires.
 *     @type string          $message_recipient The address that the email will be sent to. Defaults
 *                                              to the value of `$request->email`, but can be changed
 *                                              by the `wp_privacy_personal_data_email_to` filter.
 *     @type string          $export_file_url   The export file URL.
 *     @type string          $sitename          The site name sending the mail.
 *     @type string          $siteurl           The site URL sending the mail.
 */
$content = apply_filters( 'wp_privacy_personal_data_email_content', $email_text, $request_id, $email_data );

/**
 * Filters the headers of the email sent with a personal data export file.
 *
 * @since 5.4.0
 *
 * @param string|array $headers    The email headers.
 * @param string       $subject    The email subject.
 * @param string       $content    The email content.
 * @param int          $request_id The request ID.
 * @param array        $email_data {
 *     Data relating to the account action email.
 *
 *     @type WP_User_Request $request           User request object.
 *     @type int             $expiration        The time in seconds until the export file expires.
 *     @type string          $expiration_date   The localized date and time when the export file expires.
 *     @type string          $message_recipient The address that the email will be sent to. Defaults
 *                                              to the value of `$request->email`, but can be changed
 *                                              by the `wp_privacy_personal_data_email_to` filter.
 *     @type string          $export_file_url   The export file URL.
 *     @type string          $sitename          The site name sending the mail.
 *     @type string          $siteurl           The site URL sending the mail.
 * }
 */
$headers = apply_filters( 'wp_privacy_personal_data_email_headers', $headers, $subject, $content, $request_id, $email_data );

メール本文では以下の文字列がメール送信時にしかるべき値に置き換わります。

  • ###EXPIRATION###
  • ###LINK###
  • ###SITENAME###
  • ###SITEURL###

投稿にコメント/トラックバック/ピンバックがついたときに投稿著者に送られるメール

wp-includes/pluggable.php 内の wp_notify_postauthor 関数では、投稿にコメント/トラックバック/ピンバックがついたときに投稿著者に送られるメールをカスタマイズするためのフィルターが5つ用意されています。

  • comment_notification_recipients
  • comment_notification_notify_author
  • comment_notification_text
  • comment_notification_subject
  • comment_notification_headers
/**
 * Filters the list of email addresses to receive a comment notification.
 *
 * By default, only post authors are notified of comments. This filter allows
 * others to be added.
 *
 * @since 3.7.0
 *
 * @param string[] $emails     An array of email addresses to receive a comment notification.
 * @param string   $comment_id The comment ID as a numeric string.
 */
$emails = apply_filters( 'comment_notification_recipients', $emails, $comment->comment_ID );

/**
 * Filters whether to notify comment authors of their comments on their own posts.
 *
 * By default, comment authors aren't notified of their comments on their own
 * posts. This filter allows you to override that.
 *
 * @since 3.8.0
 *
 * @param bool   $notify     Whether to notify the post author of their own comment.
 *                           Default false.
 * @param string $comment_id The comment ID as a numeric string.
 */
$notify_author = apply_filters( 'comment_notification_notify_author', false, $comment->comment_ID );

/**
 * Filters the comment notification email text.
 *
 * @since 1.5.2
 *
 * @param string $notify_message The comment notification email text.
 * @param string $comment_id     Comment ID as a numeric string.
 */
$notify_message = apply_filters( 'comment_notification_text', $notify_message, $comment->comment_ID );

/**
 * Filters the comment notification email subject.
 *
 * @since 1.5.2
 *
 * @param string $subject    The comment notification email subject.
 * @param string $comment_id Comment ID as a numeric string.
 */
$subject = apply_filters( 'comment_notification_subject', $subject, $comment->comment_ID );

/**
 * Filters the comment notification email headers.
 *
 * @since 1.5.2
 *
 * @param string $message_headers Headers for the comment notification email.
 * @param string $comment_id      Comment ID as a numeric string.
 */
$message_headers = apply_filters( 'comment_notification_headers', $message_headers, $comment->comment_ID );

承認待ちの新しいコメント/トラックバック/ピンバックについて、サイトのモデレーターに送られるメール

wp-includes/pluggable.php 内の wp_notify_moderator 関数では、承認待ちの新しいコメントについて、サイトのモデレーターに送られるメールをカスタマイズするためのフィルターが4つ用意されています。

  • comment_moderation_recipients
  • comment_moderation_text
  • comment_moderation_subject
  • comment_moderation_headers
/**
 * Filters the list of recipients for comment moderation emails.
 *
 * @since 3.7.0
 *
 * @param string[] $emails     List of email addresses to notify for comment moderation.
 * @param int      $comment_id Comment ID.
 */
$emails = apply_filters( 'comment_moderation_recipients', $emails, $comment_id );

/**
 * Filters the comment moderation email text.
 *
 * @since 1.5.2
 *
 * @param string $notify_message Text of the comment moderation email.
 * @param int    $comment_id     Comment ID.
 */
$notify_message = apply_filters( 'comment_moderation_text', $notify_message, $comment_id );

/**
 * Filters the comment moderation email subject.
 *
 * @since 1.5.2
 *
 * @param string $subject    Subject of the comment moderation email.
 * @param int    $comment_id Comment ID.
 */
$subject = apply_filters( 'comment_moderation_subject', $subject, $comment_id );

/**
 * Filters the comment moderation email headers.
 *
 * @since 2.8.0
 *
 * @param string $message_headers Headers for the comment moderation email.
 * @param int    $comment_id      Comment ID.
 */
$message_headers = apply_filters( 'comment_moderation_headers', $message_headers, $comment_id );

ユーザーのパスワードが変更された際にサイト管理者に送られるメール

ユーザーのパスワードが変更された際にサイト管理者に送られるメールをカスタマイズするには、 wp-includes/pluggable.php 内の wp_password_change_notification 関数に用意されている wp_password_change_notification_email フィルターフックを利用します。

/**
 * Filters the contents of the password change notification email sent to the site admin.
 *
 * @since 4.9.0
 *
 * @param array   $wp_password_change_notification_email {
 *     Used to build wp_mail().
 *
 *     @type string $to      The intended recipient - site admin email address.
 *     @type string $subject The subject of the email.
 *     @type string $message The body of the email.
 *     @type string $headers The headers of the email.
 * }
 * @param WP_User $user     User object for user whose password was changed.
 * @param string  $blogname The site title.
 */
$wp_password_change_notification_email = apply_filters( 'wp_password_change_notification_email', $wp_password_change_notification_email, $user, $blogname );

ユーザーの新規登録時に管理者と新規ユーザーに送られるメール

ユーザーの新規登録時に管理者と新規ユーザーに送られるメールをカスタマイズするには、wp-includes/pluggable.php 内の wp_new_user_notification 関数に用意されている wp_new_user_notification_email_adminwp_new_user_notification_email フィルターフックを利用します。

/**
 * Filters the contents of the new user notification email sent to the site admin.
 *
 * @since 4.9.0
 *
 * @param array   $wp_new_user_notification_email_admin {
 *     Used to build wp_mail().
 *
 *     @type string $to      The intended recipient - site admin email address.
 *     @type string $subject The subject of the email.
 *     @type string $message The body of the email.
 *     @type string $headers The headers of the email.
 * }
 * @param WP_User $user     User object for new user.
 * @param string  $blogname The site title.
 */
$wp_new_user_notification_email_admin = apply_filters( 'wp_new_user_notification_email_admin', $wp_new_user_notification_email_admin, $user, $blogname );

/**
 * Filters the contents of the new user notification email sent to the new user.
 *
 * @since 4.9.0
 *
 * @param array   $wp_new_user_notification_email {
 *     Used to build wp_mail().
 *
 *     @type string $to      The intended recipient - New user email address.
 *     @type string $subject The subject of the email.
 *     @type string $message The body of the email.
 *     @type string $headers The headers of the email.
 * }
 * @param WP_User $user     User object for new user.
 * @param string  $blogname The site title.
 */
$wp_new_user_notification_email = apply_filters( 'wp_new_user_notification_email', $wp_new_user_notification_email, $user, $blogname );

すべてのメールに共通したカスタマイズを行う場合

すべてのメールに共通したメールフッターを追加するようなカスタマイズを行うには、wp-includes/pluggable.php で定義されている wp_mail 関数内に用意されているフィルターフックを利用します。

/**
 * Filters the wp_mail() arguments.
 *
 * @since 2.2.0
 *
 * @param array $args {
 *     Array of the `wp_mail()` arguments.
 *
 *     @type string|string[] $to          Array or comma-separated list of email addresses to send message.
 *     @type string          $subject     Email subject.
 *     @type string          $message     Message contents.
 *     @type string|string[] $headers     Additional headers.
 *     @type string|string[] $attachments Paths to files to attach.
 * }
 */
$atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) );

/**
 * Filters whether to preempt sending an email.
 *
 * Returning a non-null value will short-circuit {@see wp_mail()}, returning
 * that value instead. A boolean return value should be used to indicate whether
 * the email was successfully sent.
 *
 * @since 5.7.0
 *
 * @param null|bool $return Short-circuit return value.
 * @param array     $atts {
 *     Array of the `wp_mail()` arguments.
 *
 *     @type string|string[] $to          Array or comma-separated list of email addresses to send message.
 *     @type string          $subject     Email subject.
 *     @type string          $message     Message contents.
 *     @type string|string[] $headers     Additional headers.
 *     @type string|string[] $attachments Paths to files to attach.
 * }
 */
$pre_wp_mail = apply_filters( 'pre_wp_mail', null, $atts );

/**
 * Filters the email address to send from.
 *
 * @since 2.2.0
 *
 * @param string $from_email Email address to send from.
 */
$from_email = apply_filters( 'wp_mail_from', $from_email );

/**
 * Filters the name to associate with the "from" email address.
 *
 * @since 2.3.0
 *
 * @param string $from_name Name associated with the "from" email address.
 */
$from_name = apply_filters( 'wp_mail_from_name', $from_name );

/**
 * Filters the wp_mail() content type.
 *
 * @since 2.3.0
 *
 * @param string $content_type Default wp_mail() content type.
 */
$content_type = apply_filters( 'wp_mail_content_type', $content_type );

/**
 * Filters the default wp_mail() charset.
 *
 * @since 2.3.0
 *
 * @param string $charset Default email charset.
 */
$phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );

/**
 * Fires after PHPMailer is initialized.
 *
 * @since 2.2.0
 *
 * @param PHPMailer $phpmailer The PHPMailer instance (passed by reference).
 */
do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );

/**
 * Fires after PHPMailer has successfully sent an email.
 *
 * The firing of this action does not necessarily mean that the recipient(s) received the
 * email successfully. It only means that the `send` method above was able to
 * process the request without any errors.
 *
 * @since 5.9.0
 *
 * @param array $mail_data {
 *     An array containing the email recipient(s), subject, message, headers, and attachments.
 *
 *     @type string[] $to          Email addresses to send message.
 *     @type string   $subject     Email subject.
 *     @type string   $message     Message contents.
 *     @type string[] $headers     Additional headers.
 *     @type string[] $attachments Paths to files to attach.
 * }
 */
do_action( 'wp_mail_succeeded', $mail_data );

/**
 * Fires after a PHPMailer\PHPMailer\Exception is caught.
 *
 * @since 4.4.0
 *
 * @param WP_Error $error A WP_Error object with the PHPMailer\PHPMailer\Exception message, and an array
 *                        containing the mail recipient, subject, message, headers, and attachments.
 */
do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_data ) );

メール送信が正常に完了した場合に何かしたければ wp_mail_succeeded アクションフックを、メール送信がエラーになった場合に何かしたければ wp_mail_failed を使うことになります。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です