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)

[…]

 



This article was written by Latz and originally published on WP Engineer.

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

You have to agree to the comment policy.

Scroll to Top