钩子文档

password_change_email

💡 云策文档标注

概述

password_change_email 是一个 WordPress 过滤器钩子,用于自定义用户密码更改时发送的邮件内容。它允许开发者修改邮件的收件人、主题、正文和头部信息,并支持动态替换字符串如 ###USERNAME### 和 ###ADMIN_EMAIL###。

关键要点

  • 过滤器钩子:password_change_email,用于过滤密码更改邮件的构建数组。
  • 参数:$pass_change_email(数组,包含 to、subject、message、headers 等键值),$user(原始用户数组),$userdata(更新后的用户数组)。
  • 动态替换字符串:包括 ###USERNAME###、###ADMIN_EMAIL###、###EMAIL###、###SITENAME### 和 ###SITEURL###,在邮件内容中自动替换为实际值。
  • 应用场景:常用于自定义邮件内容,例如替换管理员邮箱或阻止邮件发送。

代码示例

add_filter('password_change_email', 'replace_admin_email_in_notification_emails', 10, 3);

function replace_admin_email_in_notification_emails( $pass_change_email, $user, $userdata ) {
  $pass_change_email['message'] = str_replace( '###ADMIN_EMAIL###', 'other_email@your-domain.com', $pass_change_email['message'] );
  return $pass_change_email;
}

注意事项

  • 使用 add_filter( 'password_change_email', '__return_false' ) 可以阻止发送密码更改邮件,但更推荐使用 send_password_change_email 钩子来控制邮件发送行为。
  • 此钩子自 WordPress 4.3.0 版本引入。

📄 原文内容

Filters the contents of the email sent when the user’s password is changed.

Parameters

$pass_change_emailarray
Used to build wp_mail() .

  • to string
    The intended recipients. Add emails in a comma separated string.
  • subject string
    The subject of the email.
  • message string
    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.
  • headers string
    Headers. Add headers in a newline (rn) separated string.

$userarray
The original user array.
$userdataarray
The updated user array.

Source

$pass_change_email = apply_filters( 'password_change_email', $pass_change_email, $user, $userdata );

Changelog

Version Description
4.3.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Use the code below if you don’t wish to show your real administrator email inside password change notifications.

    Change ‘other_email@your-domain.com’ to the new email address.

    /**
     * Change admin email in notifications.
     *
     * This applies to password change notifications.
     *
     * @param (array) $pass_change_email Used to build wp_mail().
     * @param (array) The original user array.
     * @param (array) The updated user array.
     *
     * @return (array) $pass_change_email Updated wp_mail() content.
     */
    add_filter('password_change_email', 'replace_admin_email_in_notification_emails', 10, 3);
    
    function replace_admin_email_in_notification_emails( $pass_change_email, $user, $userdata ) {
      $pass_change_email['message'] = str_replace( '###ADMIN_EMAIL###', 'other_email@your-domain.com', $pass_change_email['message'] );
    
      return $pass_change_email;
    }