How to Change the Welcome User Email

I recently needed to customize the welcome new user email that goes out when WordPress registers a new user. While there are several plugins that handle that, I wanted to learn how to do myself (if anything, just to learn). After comparing several plugins and code I found on StackExchange, I came up with this plugin.

<?php

/*
Plugin Name: Slushman Welcome Email
Plugin URI: http://slushman.com/plugins/slushman-welcome-email
Description: Customize the output of the welcome email sent by WordPress.
Version: 0.1
Author: Slushman
Author URI: http://slushman.com
License: GPLv2

**************************************************************************

  Copyright (C) 2011 Slushman

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General License for more details.

  You should have received a copy of the GNU General License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.

**************************************************************************

*/

// Redefine user notification function
if ( !function_exists( 'wp_new_user_notification' ) ) {

    function wp_new_user_notification( $studentID, $plaintext_pass = '' ) {

		// URLs
		$site_url = site_url();
		$seminar_url = site_url( 'seminars/' );
		$login_url = wp_login_url();
		
		// Email variables
		$student 			= new WP_User($studentID);
		$student_data 		= get_userdata( $studentID );
		$firstname 			= $student_data->first_name;
		$student_login 		= stripslashes( $student_data->user_login );
		$headers 			= 'From: CEMB Seminar System - DO NOT REPLY <seminars@curbcollege.info>' . "rn";
		$blog_name 			= get_option( 'blogname' );
		$admin_subject 		= 'New User Registration on ' . $blog_name;
		$welcome_subject 	= 'Welcome to the CEMB Seminar system!';
		$welcome_email		= stripslashes( $student_data->user_email );
		$admin_email		= get_option('admin_email');
		
		$admin_message = 
<<<EOT

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	</head>
	<body>
		<div class="content">
			<div class="wrapper">
				<p>New user registration on your blog: {$blog_name}.</p>
				<p>Username: {$student_login}</p>
				<p>Email: {$welcome_email}</p>
			</div><!-- End of wrapper -->
		</div><!-- End of content -->
	</body>
</html>

EOT;
	
		$welcome_message = 
<<<EOT
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	</head>
	<body>
		<div class="content">
			<div class="wrapper">
				<table width="100%" border="0" cellspacing="0" cellpadding="0">
					<tr>
						<td align="center" valign="middle" bgcolor="#141415"><img src="http://makemeanexpert.com/beta.curbcollege.info/wp-content/uploads/2011/08/header.jpg" /></td>
					</tr>
					<tr>
						<td><br /><br />
							Hello {$firstname},<br />
							<br />
							Welcome to the new CEMB Seminar system! You can log in to change your password and email address, as well as see your seminar credits and which seminars you've attended.  When you receive credit for attending a seminar, you will recieve an email confirming your attendance credit, how many credits you've earned so far, and any upcoming seminars (if you still need some credits).<br />
							<br />
							To log into your account, go <a href="{$login_url}">visit the new seminar site</a> and use the credentials below.<br />
							<br />
							Your Username: {$student_login}<br />
							Your Password: {$plaintext_pass}<br />
							<br />
							See you at seminar!<br /><br />
						</td>
					</tr>
					<tr>
						<td style="background-color:#141415;">
							<table width="100%" border="0" cellspacing="0" cellpadding="0">
								<tr>
									<td colspan="3">
										<center><img src="http://makemeanexpert.com/beta.curbcollege.info/wp-content/uploads/2011/08/emailfooter.jpg" style="width:700px;height:80px;" style="align:center;" /></center>
									</td>
								</tr>
								<tr>
									<td align="right" valign="center">
										<a href="{$site_url}" style="color:#fffffe !important;width:234px;">Curb College Info</a>
									</td>
									<td align="center" valign="center">
										<a href="{$seminar_url}" style="color:#fffffe !important;width:234px;">Seminar Page</a>
									</td>
									<td align="left" valign="center">
										<a href="{$login_url}" style="color:#fffffe !important;width:234px;">Seminar Login</a>
									</td>
								</tr>
							</table>
						</td>
					</tr>
				</table>
			</div><!-- End of wrapper -->
		</div><!-- End of content -->
	</body>
</html>
EOT;

		wp_mail( $admin_email, $admin_subject, $admin_message, $headers );
		wp_mail( $welcome_email, $welcome_subject, $welcome_message, $headers );

	} // End of wp_new_user_notification()
	
} // End of 'if wp_new_user_notification doesn't exist'
	
?>

Thankfully, wp_new_user_notification is a pluggable function, which means it can be overridden with a plugin. This simple plugin replaces that function and allows you to customize the HTML however you need it displayed. There’s no admin page for it, you simply edit the HTML in the file itself, save it (or update it if you’re using the Plugin Editor in WordPress), and your new welcome message will be shown when a new user is registered.

I chose to use the heredoc technique for the message for the simplicity. Any PHP variables you surround with brackets and put in the HTML will be replaced with the variable’s value, so it’s much simpler to figure out how the message will appear to the user.