Customizing the User Registration Notification eMails

If a new user registers at a WordPress site the new user and the administrator receive notification emails:

User:

From: myBlog (info@myBlog.com)
Subject: [myBlog] Your username and password info

Username: new_user

To set your password, visit the following address:<http://myblog/wp-login.php?action=rp&key=3oCJkevP1ZSSb0P8DlOW&login=new_user>

http://myblog/wp-login.php

Admin:

From: myBlog (info@myBlog.com)
Subject: [myBlog] New User Registration

New user registration on your site myBlog:

Username: new_user

Email: new_user@myblog.com

Until version 4.8 the content of the mail was hard coded. The only way to alter the emails was to hook into wp_mail() or even phpmailer.

WordPress v4.9 now offers the ability to easily customize these emails by using the following filters:

The filters fire after the user has been added to the database.

The filters for the user’s and the admin’s email follow the same logic, just the filter and the variable names differ:

$wp_new_user_notification_email_admin = apply_filters( 'wp_new_user_notification_email_admin', $wp_new_user_notification_email_admin, $user, $blogname )

The parameters contain the following values:

  • $wp_new_user_notification_email_admin: Associative array with the keys to, subject, message, headers.
  • $user: A WP_User object of the registered user.
  • $blogname: A string containing the name of the blog the user registered to.

With these values at hand it’s easy to create your customized mail:

add_action( 'login_init', 'my_wp_new_user_notification_email_admin_init' );

function my_wp_new_user_notification_email_admin_init() {
    add_filter( 'wp_new_user_notification_email_admin', 'my_wp_new_user_notification_email_admin', 10, 3 );
}

function my_wp_new_user_notification_email_admin( $wp_new_user_notification_email, $user, $blogname ) {
    $wp_new_user_notification_email['subject'] = sprintf( '[%s] New user %s registered.', $blogname, $user->user_login );
    $wp_new_user_notification_email['message'] = sprintf( "%s ( %s ) has registerd to your blog %s.", $user->user_login, $user->user_email, $blogname );

    return $wp_new_user_notification_email;

}
(As always for filters: do not forget to return the altered variable.)  The mail now looks like this:

From: myBlog (MyBlog@myblog.com)
Subject: [myBlog] New user new_user registered.

new_user (new_user@user.com) has registerd to your blog myBlog.

Of course, you can insert more data than is provided by the filter. E.g., you can tell the admin, how many registered users the blog already has:

function my_wp_new_user_notification_email_admin($wp_new_user_notification_email, $user, $blogname) {

    $user_count = count_users();

    $wp_new_user_notification_email['subject'] = sprintf('[%s] New user %s registered.',
$blogname, $user->user_login);
    $wp_new_user_notification_email['message'] =
    sprintf( "%s has registerd to your blog %s.", $user->user_login, $blogname) .
"\n\n\r" . sprintf("This is your %d. user!", $user_count['total_users']);

    return $wp_new_user_notification_email;

}
The parameter headers can be used to change some header information easily:

In this example, the From information of the mail is changed and the user admin2@myblog is added as an additional recipient. As you can see, multiple headers entries are separated by "\n\r" (return and linefeed).
$wp_new_user_notification_email['headers'] = "From: myBlog <myblog@myblog.com> \n\r cc: Admin 2 <admin2@mblog.com>";

As said before, this all could already be done by using wp_mail() and phpmailer but the new filters are way more convenient.

Note. The strings are \n and \r in the code means.

  • \r is the carriage return
  • \n is the new line

Posted

in

by

Tags:

Comments

One response to “Customizing the User Registration Notification eMails”

  1. Collins Agbonghama Avatar

    I will like to mention that if you have a plugin targeting users with versions less than 4.9 like myself, you can still do it the old way by redeclaring wp_new_user_notification function since it’s a pluggable function.