Customizing the “New User Registration” and “Your username and password” email notifications sent by WordPress

When a new user is registered, WordPress sends a New User Registration notification to the admin email address in the site’s General Settings. A Your username and password email is also sent to the new user.

This functionality is handled by wp_new_user_notification() located in wp-includes/pluggable.php.

<?php
if ( !function_exists('wp_new_user_notification') ) :
/**
* Notify the blog admin of a new user, normally via email.
*
* @since 2.0
*
* @param int $user_id User ID
* @param string $plaintext_pass Optional. The user's plaintext password
*/
function wp_new_user_notification($user_id, $plaintext_pass = '') {
    $user = get_userdata( $user_id );

    // The blogname option is escaped with esc_html on the way into the database in sanitize_option
    // we want to reverse this for the plain text arena of emails.
    $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);

    $message = sprintf(__('New user registration on your site %s:'), $blogname) . "\r\n\r\n";
    $message .= sprintf(__('Username: %s'), $user->user_login) . "\r\n\r\n";
    $message .= sprintf(__('E-mail: %s'), $user->user_email) . "\r\n";

    @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message);

    if ( empty($plaintext_pass) )
        return;

    $message = sprintf(__('Username: %s'), $user->user_login) . "\r\n";
    $message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
    $message .= wp_login_url() . "\r\n";

    wp_mail($user->user_email, sprintf(__('[%s] Your username and password'), $blogname), $message);

}
endif;
?>

 

This is a pluggable function so it can be customized by redefining it . Unfortunately redefining it in your theme has no effect, it needs to be replaced via a plugin.

It seems that plugins are loaded first, then pluggable.php, then the theme. So if your function override is in your theme, you’ll clash with the default function. – Steve Taylor

Here’s a quick plugin template for customizing the notifications.

<?php
/**
 * Plugin Name: Custom New User Notifications
 * Description: Customize the "New User Registration" and "Your username and password" notifications
 * Version: 1.0
 * Author: Ben Pearson
 * Author URI: http://benpearson.com.au
 */

if ( !function_exists('wp_new_user_notification') ) :
/**
 * Notify the blog admin of a new user, normally via email.
 *
 * @since 2.0
 *
 * @param int $user_id User ID
 * @param string $plaintext_pass Optional. The user's plaintext password
 */
function wp_new_user_notification($user_id, $plaintext_pass = '') {
    $user = get_userdata( $user_id );

    // The blogname option is escaped with esc_html on the way into the database in sanitize_option
    // we want to reverse this for the plain text arena of emails.
    $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);

    $message  = sprintf(__('New user registration on your site %s:'), $blogname) . "\r\n\r\n";
    $message .= sprintf(__('Username: %s'), $user->user_login) . "\r\n\r\n";
    $message .= sprintf(__('E-mail: %s'), $user->user_email) . "\r\n";

    @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message);

    if ( empty($plaintext_pass) )
        return;

    $message  = sprintf(__('Username: %s'), $user->user_login) . "\r\n";
    $message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
    $message .= wp_login_url() . "\r\n";

    wp_mail($user->user_email, sprintf(__('[%s] Your username and password'), $blogname), $message);

}
endif;
?>

Include the function_exists() check it’s wrapped in. If you include this, your function will still get priority over the WP core; it’ll just prevent fatal errors if you activate a plugin that happens to have overridden the same function. – Steve Taylor

Resources

7 thoughts on “Customizing the “New User Registration” and “Your username and password” email notifications sent by WordPress

  1. afslankpillen

    G’Day! Ben Pearson,
    Interesting Post, I would like to set up a blog similar to Apartmenttherapy.com or like the Sugar Networks (buzzsugar.com/casasugar.com/etc.).

    They are basically separate blogs on one site (readers can leave comments using the SAME registration across all sites on network).

    I currently have one blog up and running using a single wordpress installation. I would like to (like the example websites above) host several blogs or categories on one site (so that users don’t have to register for a different username/password for each blog. I also would like each blog/category to have a unique header and background.

    Is there a way to do this using a single installation of wordpress or should I try to figure out how to use WordPress MU?

    The easiest possibility I was thinking of:

    – Creating “category” pages as the separate blogs (I’m not sure how to customize).

    Thanks in advanced!
    BTW great blogpost

    Reply
    1. Ben Pearson Post author

      Hello. Yes, you could do this with just categories. Here’s a good place to start in the Codex.

      I did something similar on this site. If you click on a post you can see the different categories have different headers and sidebar content eg. Category A and Category B.

      Reply
  2. Mark Slatem

    Hi there Ben.

    Do you know how on earth to change the new user registration email default “from email address” as well as the “from name” ?

    It is relatively easy to do this for other wordpress emails via functions file and it does work for other notifications (comments etc) but it does not work for new user registrations.

    Any clue how to change this?

    Thanks

    Mark

    BTW I am using WordPress 3.9.x and the Genesis framework

    Reply
  3. webipgroup

    Hi!
    Maybe this can solve my problem, but I need to ask you something first: I have an e-commerce based on WooCommerce and is mandatory for a user to insert his VAT number on registration. I want that the “New user registration” email contains even the VAT number. How can I do that?
    Thanks!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *