How to add usermeta to “Notice of Email Change” email message

The filter to use to modify the e-mail sent to a user when they change their e-mail address is email_change_email. Note that this is different from password_change_email which you’ve used in your original code. That filter allows you to modify the e-mail sent to the user when their password is changed.

These two filters work similarly but it’s important to make a distinction between the two. Both filters appear in wp-includes/user.php.

  • In the code below, we use the email_change_email to modify the message text. The new placeholders, ###FIRST_NAME### and ###LAST_NAME### have been added to the code (and docs) below.

  • The standard placeholders wherever possible in the message text, instead of hardcoding the strings.

  • Also, a text domain was added to the customized message text. It’s always a good practice to add a text domain to strings that are passed to gettext functions ( __(), _e(), etc.).

/**
 * 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;
}

Debugging

I’ve verified that this code does output the first and last names of the user. This information is coming from the user meta table, but it’s already set up for us via the $user array by the core. I’ve manually replaced any personal info with**REMOVED**.

Example e-mail:

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

Here’s the contents of the $user array:

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
    How to add usermeta to "Notice of Email Change" email message => 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] => 
)

Here’s a function that will display the $user array in the console of the admin area once the debugging line has been activated in the original code above.

/**
 * 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>';
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)