如何在“电子邮件更改通知”电子邮件中添加用户

时间:2017-06-06 作者:DigitalDesigner

希望有人能帮忙。

更新电子邮件地址时,我正在尝试更改您收到的电子邮件中的“电子邮件更改通知”文本。

这是我目前掌握的

add_filter( \'password_change_email\', \'red_change_password_mail_message\', 10, 3 );

function red_change_password_mail_message( $pass_change_mail, $user, $userdata ) {

 $new_message_txt = __( \'Hi [first_name] [last_name], 

 This notice confirms that your email was changed on IBEW Local 353.

 If you did not change your email, please contact the Site Administrator at [email protected]

 This email has been sent to [user_email]

 Regards,
 All at IBEW Local 353\' );
 $pass_change_mail[ \'message\' ] = $new_message_txt;
 return $pass_change_mail;

}
正如你所看到的,我想用他们的名字和姓氏对电子邮件进行个性化设置。做这件事的最好方法是什么?谁能给我举个例子让我开始学习呢。

2 个回复
最合适的回答,由SO网友:Dave Romsey 整理而成

当用户更改其电子邮件地址时,用于修改发送给用户的电子邮件的筛选器为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>\';
}

SO网友:WebElaine

你已经通过了$user 对象,因此您应该能够在$user->first_name$user->last_name.

所以只要改变$new_message_txt 收件人:

$new_message_txt = __(\'Hi \' . $user->first_name \' . \' \' . $user->last_name . \',
...(rest of your message)
... \' . $pass_change_mail . \'... end of your message\');

结束

相关推荐

apostrophe in email name

我正试图邀请某人成为Wordpress私人博客的用户,但WP显然不接受电子邮件地址中出现的撇号有效。例如,它似乎会考虑maryo的[email protected]由于玛丽姓中的撇号,邮件地址无效。还有其他人遇到过这个问题吗?有没有办法用不同的字符来代替撇号?