Customizing the User Registration Notification eMails – WP Engineer

Customizing the User Registration Notification eMails - WP Engineer

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

User:

From: myBlog ([email protected])
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&amp;key=3oCJkevP1ZSSb0P8DlOW&amp;login=new_user>

http://myblog/wp-login.php

Admin:

From: myBlog ([email protected])
Subject: [myBlog] New User Registration

New user registration on your site myBlog:

Username: new_user

Email: [email protected]

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

WordPress v4.9 now offers the ability to easily customize these mails 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 ([email protected])
Subject: [myBlog] New user new_user registered.

new_user ([email protected]) has registerd to your blog myBlog.

Of course, you can insert more data than 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) .
"nnr" . sprintf("This is your %d. user!", $user_count['total_users']);

    return $wp_new_user_notification_email;

}

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

$wp_new_user_notification_email['headers'] = "From: myBlog <[email protected]> nr cc: Admin 2 <[email protected]>";

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 “nr” (return and linefeed).

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

Keep reading the article at WP Engineer. The article was originally written by Latz on 2018-01-16 05:07:25.

The article was hand-picked and curated for you by the Editorial Team of WP Archives.

Disclosure: Some of the links in this post are "affiliate links." This means if you click on the link and purchase the product, We may receive an affiliate commission.

Leave a Comment

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

Show Your ❤️ Love! Like Us
Scroll to Top