WordPress

Change Default WordPress Emails

One of the best things about WordPress development is that not only can you use it as a CMS but as an application framework, it comes with a huge selection of utility functions that make it easy to handle form helpers, validation, file structures, email encoding and more.

With WordPress actions and filters it becomes a very extendible framework to work with, another way to extend WordPress is with pluggable functions, which allows you to override the core WordPress functions with any code you want. The pluggable functions can be included in the functions.php file or in your own plugins to produce new functionality for the functions already used in WordPress.

The pluggable functions file is located at /wp-includes/pluggable.php and will have functions to handle:

  • Getting user information.
  • Sending emails
  • Authenticate the WordPress admin area login.
  • Logout the user
  • Validation cookie information
  • Authorise redirects on logins
  • Email notifications to admin and authors
  • Hash passwords

In this tutorial we are going to investigate the emails that WordPress sends out to authors, admins and registered users. We are going to find out what functions we need to override to change the email that WordPress sends out to the user. You can use this to brand the email to registered users for your website or add any additional information that you need in the email.

To send an email with WordPress you just need to use the built in function wp_mail(), this is a wrapper for the php mail() function, but allows you to use WordPress filters to change the content returned all the emails sent from your site..

WordPress Mail

New User Notification

When a new user signs up to your WordPress site an email is sent out to the admin user to notify them that a new user has registered. The function will also send an email to the new user with their username, new generated password and a link to the login page to make it easy to sign in.

But if you don't want the user to get an email with their login information, or if you want to send this to the admin user before they get the password or for any other reason to customise this process, you will need to change this function.

Because this is a pluggable function and is wrapped in a if function_exists then we can override this function in a plugin or functions.php file. To override this function create a new plugin and add the function wp_new_user_notification($user_id, $plaintext_pass = ''), this will now override the default WordPress function so you can do whatever you want in this code.

function wp_new_user_notification($user_id, $plaintext_pass = '') 
{
    // Change the code to customise as you want
}

To add a message to the user email and change the link to the login page you can use the following function.

function wp_new_user_notification($user_id, $plaintext_pass = '') {
	$user = get_userdata( $user_id );

	// The blogname option is escaped with esc_html on the way into the database in sanitize_option
	// we want to reverse this for the plain text arena of emails.
	$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);

	$message  = sprintf(__('New user registration on your site %s:'), $blogname) . "\r\n\r\n";
	$message .= sprintf(__('Username: %s'), $user->user_login) . "\r\n\r\n";
	$message .= sprintf(__('E-mail: %s'), $user->user_email) . "\r\n";

	@wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message);

	if ( empty($plaintext_pass) )
		return;

	$message  = sprintf(__('Username: %s'), $user->user_login) . "\r\n";
	$message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
	$message .= 'To log into the admin area please us the following address ' . wp_login_url() . "\r\n";

	wp_mail($user->user_email, sprintf(__('[%s] Your username and password'), $blogname), $message);

}

Change Notification Of Comment Email

Whenever a visitor leaves a comment on a post WordPress can be set to send an email to the author to notify them they need to moderate the comment. This is another pluggable function that you can override to customise how you want it in your email.

This function will work for trackbacks, pingbacks and comments. A common thing to change for this is to email trackbacks and pingbacks to the site admin and just send the author the notifications to comments.

To override this function create a new plugin and add the following function.

function wp_notify_postauthor( $comment_id ) {
    // enter new code for email that notifies the post author
}

If you want to change the email that gets sent to the moderator when a comment is made you can override the function wp_notify_moderator().

function wp_notify_moderator($comment_id) {
    // enter new code to change the email that notifies moderators
}

Change Notification Of Password Change Email

When the user is on the login page and requests a reset of password then an email is sent to the admin user to let them know which user is resetting their password.

If you want to change this email with more information on the user resetting their password then you can do so by overriding this function.

Create a new plugin and add the function wp_password_change_notification().

function wp_password_change_notification( $user )
{

}

More Pluggable Functions

To view more inbuilt WordPress functions you are able to override view the pluggable.php file in wp-includes/ folder.

Back to top