When a user registers on your WordPress site or you create a user manually in wp-admin an email is sent automatically. In many cases this email will not be adequate; you may want to add a link, customize some of the copy, or create an HTML email with images.
The solution is to change the WordPress default implementation of the wp_new_user_notification() function. wp_new_user_notification() is a pluggable function which means you’ll need to create a plugin in order to redefine it.
Get Started, Create a Plugin
The first step in modifying the email is to create our plugin. Simply create a directory in wp-content/plugins/ called user-emails, then create a file within the directory called user-emails.php. Open user-emails and add this to the top:
// wp-content/plugins/user-emails/user-emails.php <?php /* Plugin Name: User Emails Description: Changes the default user emails Version: 1.0 Author: Joe Sexton Author URI: http://www.josephmsexton.com */
Your plugin should be working and you should be able to go to the wp-admin plugins menu and activate it.
Define wp_new_user_notification()
The next step is to update wp_new_user_notification() to make a few changes. The original function is defined in wp-includes/pluggable.php. Start by defining the function like this:
// wp-content/plugins/user-emails/user-emails.php /** * redefine new user notification function * * emails new users their login info * * @author Joe Sexton <joe@webtipblog.com> * @param integer $user_id user id * @param string $plaintext_pass optional password */ if ( !function_exists( 'wp_new_user_notification' ) ) { function wp_new_user_notification( $user_id, $plaintext_pass = '' ) { } }
now when users register nothing will happen since we defined the function to do nothing. Using the original function as a reference we can create something similar, but with a twist; we’ll create an html email to send to the user along with a logo for the site.
// wp-content/plugins/user-emails/user-emails.php /** * redefine new user notification function * * emails new users their login info * * @author Joe Sexton <joe@webtipblog.com> * @param integer $user_id user id * @param string $plaintext_pass optional password */ if ( !function_exists( 'wp_new_user_notification' ) ) { function wp_new_user_notification( $user_id, $plaintext_pass = '' ) { // set content type to html add_filter( 'wp_mail_content_type', 'wpmail_content_type' ); // user $user = new WP_User( $user_id ); $userEmail = stripslashes( $user->user_email ); $siteUrl = get_site_url(); $logoUrl = plugin_dir_url( __FILE__ ).'/sitelogo.gif'; $subject = 'Welcome to MySite.com'; $headers = 'From: MySite <noreply@mysite.com>'; // admin email $message = "A new user has been created"."\r\n\r\n"; $message .= 'Email: '.$userEmail."\r\n"; @wp_mail( get_option( 'admin_email' ), 'New User Created', $message, $headers ); ob_start(); include plugin_dir_path( __FILE__ ).'/email_welcome.php'; $message = ob_get_contents(); ob_end_clean(); @wp_mail( $userEmail, $subject, $message, $headers ); // remove html content type remove_filter ( 'wp_mail_content_type', 'wpmail_content_type' ); } } /** * wpmail_content_type * allow html emails * * @author Joe Sexton <joe@webtipblog.com> * @return string */ function wpmail_content_type() { return 'text/html'; }
The first step is to filter wp_mail_content_type to return text/html; this allows us to send html emails. Then we get the user object using the id, this way we can get user meta such as their first name or email address. We set some other variables as well such as the website url and the url for the site’s logo. Next we send the admin email, leave this step out if you like. After the admin email is sent we include the email_welcome.php file; this will be the html email template that we will send. After sending the email the final step is to remove the wp_mail_content_type filter so emails get sent out using the default type.
Next Steps
First add the sitelogo.gif file to the wp-content/plugins/user-emails directory. Next create a new file in the wp-content/plugins/user-emails directory called email_welcome.php. This file is the template for the email that will be sent out and can be totally custom, whatever you want for your site. This example should get you started:
<!-- wp-content/plugins/user-emails/email_welcome.php --> <img src="<?php echo $logoUrl; ?>" alt="MySite"/> <?php if ( $user->first_name != '' ) : ?> <h1><?php echo $user->first_name; ?>, Welcome to MySite</h1> <?php else : ?> <h1>Welcome to MySite</h1> <?php endif; ?> <p> We're glad you're here. It's nice to have friends. </p> <p> <a href="<?php echo $siteUrl; ?>">MySite</a> </p> <p> Thank you,<br> MySite </p>
That should be it, now when a user is created on your site the email sent will contain the new HTML markup, including the site logo that was added.
This is great! Thanks so much for sharing. Only one issue though, when a new user gets an email, the username being sent is the user ID, which is not helpful to them, they want the user login name. What needs to be changed in user-emails.php to do this? Thanks!
Hi,
I was using your code for a while and it was working great!
But it isn’t working anymore.. do you have any idea how that is possible? WordPress updates maybe!?
Thanks!
Just wanted to say a huge thank you. I’ve been trying to accomplish this for a very long time. All the other plugins / tutorials didn’t work for me. But yours did : )
I was wondering if it was possible to completely remove the default email that new users get. (Currently following your tutorial the new user receives the default email plus this nice new customised one.) If we were going to achieve that we would need to put user password into the customised email. I didn’t have any success with that using $user->user_pass — all I got was a hashed value.
Anyway, thanks again!
To send with the user name and password.
User variables like $user->user-login and $plaintext_pass in your email_welcome.php file.
Otherwise thanks Joe
very helpful, but i use arabic as a language is there a way around that?
I just want to send different emails to different users according to different role like customer, vendor , officiator etc for woo commerce site how it possible.
you can try doing consitionals per role like this:
if(isset($user->roles[0])){
if($user->roles[0] == ‘vendor’){
// do something
}
This a great plugin! I only don’t get the $plaintext_pass to work. No matter what I try, $user->user_pass, $user->user_password, the password doesn’t show in the email. What am I doing wrong?
This is not working in latest version of WordPress, please guide