当用户更改其电子邮件地址时,用于修改发送给用户的电子邮件的筛选器为email_change_email
. 请注意,这与password_change_email
您在原始代码中使用的。该过滤器允许您在更改用户密码时修改发送给用户的电子邮件。
这两个过滤器的工作原理相似,但区分这两个过滤器很重要。两个过滤器都出现在wp-includes/user.php
.
在下面的代码中,我们使用email_change_email
修改消息文本。新占位符,###FIRST_NAME###
和###LAST_NAME###
已添加到下面的代码(和文档)中。
尽可能在消息文本中使用标准占位符,而不是对字符串进行硬编码。
此外,在自定义消息文本中添加了文本域。将文本域添加到传递给gettext函数的字符串中总是一种好的做法(__()
, _e()
, 等等)。
/**
* Filters the contents of the email sent when the user\'s email is changed.
*
* @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.
* - ###FIRST_NAME### The current user\'s first name.
* - ###LAST_NAME### The current user\'s last name.
* - ###ADMIN_EMAIL### The admin email in case this was unexpected.
* - ###EMAIL### The old email.
* - ###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.
*/
add_filter( \'email_change_email\', \'red_email_change_email\', 10, 3 );
function red_email_change_email( $email_change_email, $user, $userdata ) {
$new_message_txt = __( \'Hi ###FIRST_NAME### ###LAST_NAME###,
This notice confirms that your email was changed on ###SITENAME###.
If you did not change your email, please contact the Site Administrator at
###ADMIN_EMAIL###
This email has been sent to ###EMAIL###
Regards,
All at ###SITENAME###\' );
$email_change_email[\'message\'] = $new_message_txt;
$email_change_email[\'message\'] = str_replace( \'###FIRST_NAME###\', $user[\'first_name\'], $email_change_email[\'message\'] );
$email_change_email[\'message\'] = str_replace( \'###LAST_NAME###\', $user[\'last_name\'], $email_change_email[\'message\'] );
// Debugging helper. Uncomment to turn on.
// update_option( \'wpse_debug_email_change_email_user\', $user );
return $email_change_email;
}
调试我已经验证了这段代码是否输出了用户的名字和姓氏。此信息来自用户元表,但已经通过
$user
按核心排列。我已手动将任何个人信息替换为
**REMOVED**
.
电子邮件示例:
Hi Dave (first name) Romsey (last name),
This notice confirms that your email was changed on WP Theme Testing.
If you did not change your email, please contact the Site Administrator at
**REMOVED**
This email has been sent to **REMOVED**
Regards,
All at WP Theme Testing
以下是
$user
阵列:
Array
(
[ID] => 1
[user_login] => dave
[user_pass] => **REMOVED**
[user_nicename] => dave
[user_email] => **REMOVED**
[user_url] => http://example.com/
[user_registered] => 2016-02-14 05:29:13
[user_activation_key] =>
[user_status] => 0
[display_name] => dave
[first_name] => Dave (first name)
[last_name] => Romsey (last name)
[nickname] => dave
[description] => This is the author’s test! <a href=\\"#\\">test link</a>
[rich_editing] => true
[comment_shortcuts] => false
[admin_color] => fresh
[use_ssl] => 0
[show_admin_bar_front] => true
[locale] =>
)
下面是一个函数,它将显示
$user
在上面的原始代码中激活调试行后,管理区域控制台中的数组。
/**
* Debugging helper. Outputs $user array to console in admin area.
* This data is saved via debugging line in red_email_change_email().
*/
add_action( \'admin_head\', \'wpse_debug_option\' );
function wpse_debug_option() {
$value = get_option( \'wpse_debug_email_change_email_user\' );
echo \'<script>console.log( \' . json_encode( $value ) . \');</script>\';
}