Change the default WordPress account activation email: subject, message and sender
When I set out to change the settings for the default WordPress activation email subject, message and sender, I thought it would be a pretty obvious thing to change. I was wrong. Here’s what happens and how to customize it:
When a new user signs up a new WordPress account, they need to activate it and they receive a standard message from WordPress.
The email activation message says:
From: WordPress (wordpress@mysite.com)
To activate your user, please click the following link:
(Link)
After you activate, you will receive *another email* with your login.
(Note: The following solution only customizes the activation email, it does not trigger an account activation process. You have to set up an activate by email condition, for example by using Gravity Forms with the User Registration add on.)
First, I didn’t want my messages to be from: WordPress. I want it to be from me, or my client’s name. In addition to being impersonal, its confusing if they didn’t know they were on a WordPress site. Also, the from email should be from a specific email address, not wordpress@mysite.com.
Next, I want the Subject also to be more personalized, and finally I wanted to customize the actual message in the email.
One would hope that there would be a simple configuration setting to change this, however that is not the case.
Using WordPress Functions and Filters
Digging around the web reveals a few hints in the form of WordPress core functions and filters:
- wpmu_signup_user_notification:
- wpmu_signup_user_notification_email
- wpmu_signup_user_notification_subject
- wp_mail_from
- wp_mail_from_name
Using these it is possible to put together some filters that modify the output of the WordPress activation email:
Code to Customize the WordPress activation emails:
//First we'll setup the from name and sender email.
//Here is the filter for the from name:
add_filter('wp_mail_from', 'new_mail_from');
//Here is the filter for the sender name:
add_filter('wp_mail_from_name', 'new_mail_from_name');
//Here is where the new from email goes:
function new_mail_from($old) {
return 'MyNewEmail@mydomain.com';
}
//Here is where the new sender name goes:
function new_mail_from_name($old) {
return 'New Sender Name';
}
//Next we'll hook into the subject and setup a function to change it:
add_filter( 'wpmu_signup_user_notification_subject', 'my_activation_subject', 10, 4 );
function my_activation_subject( $text ) {
//Here is where to input the new subject for the activation email:
return 'Customize me: Your account needs activation.';
}
// Finally we hook into the email itself and run a function to modify the message.
add_filter('wpmu_signup_user_notification_email', 'my_custom_email_message', 10, 4);
function my_custom_email_message($message, $user, $user_email, $key) {
//Here is the new message:
$message = sprintf(__(( "To activate your new account, please click the following link:\n\n%s\n\n After you activate you will be able to log in.\n\n" ),
$user, $user_email, $key, $meta),site_url( "?page=gf_activation&key=$key" ));
return sprintf($message);
}
Paste all of this into your functions.php file or create a quick plugin by adding this before the code: (Don’t forget opening and closing php tags.)
/**
* Plugin Name: Custom Activation Emails
* Description: There are no options, modify the plugin directly to change things.
*/
Put it into a folder and upload to plugins, or zip it and install it via plugins.
That’s it, now we have custom email activation from name, from email, subject and message.
Aaron,
A client of mine asked me to change the activation email that was being generated (I thought) by Gravity Forms, it turns out it’s not, GF use the default WP activation emails.
Your solution above is brilliant, I’ve wrapped it up in a little plugin, altered the text to what my client wants and Voila! All is good again :o)
Thank you so much – awesome work.
Excellent! Glad it helped you out.
Hi Aaron, I used your idea above and all worked fine except for the activation key $key
This returns an empty value.
I’ve fiddled with the function made $key global and come to the conclusion it just is not available at the time the function executes, At one point I created a new key which printed in the emails but alas I could not update the database, I may try this again.
add_filter(‘wpmu_signup_user_notification_email’, ‘my_custom_email_message’, 10, 4);
function my_custom_email_message($message, $user, $user_email, $key) {
global $key, $user, $user_email, $wpdb; $message;
$keyarray = $wpdb->get_results( “SELECT activation_key FROM wpbb_signups WHERE user_email = ‘$user_email'”, ARRAY_A );
$key = $keyarray[‘activation_key’];
// $key = $keyarray->activation_key; also tried
//$key = “12345”; this prints in mail.
$message = “This is my message \n\n Link 1:http://www.brightbell.com/photo2 \n\n Link 2: \n\n http://www.brightbell.com/wp-login.php?cimy_key=“. $key . “\n\n User is ” . $user_email;
return sprintf($message);
I know $user_mail is a there because it will print out in the email but not $key although if I do a simple assign to a value $key will print this out. So it appears my SQL query is failing. I know its in the database I have looked in PHPMYADMIN. I am not used to doing queries in a WordPress environment am I doing something wrong to get $key.
Thanks
Hi Ian,
This code only edits the activation email, it does not actually trigger an activation process. For example you can use Gravity Forms + the User Registration add-on to trigger an activate by email condition. Once activation is set up then the ‘$key’ will be automatically generated and sent out. However, if you are using the regular WordPress registration process, then there is no key generated, thus the empty string.
Hope that helps,
Best,
Aaron
Hello Aaron,
Thank you very much for the code. When I implemented it, and it worked as expected to change the activation email. I installed it as a plugin. The problem is when I install and activate the custom email activation plugin the login/logout redirection behavior of the site stops working. I also get a blank screen when trying to access the admin area.
I am using WordPress v. 4.0 and GravityForms 1.8.
Do you have any suggestions as to what may be causing this to happen?
~David Kuhn
Hi David,
Since the code is working from functions.php and the code itself is not doing anything but filtering content, I can only think of a few reasons this might happen when used as a plugin. (It works fine for me in 4.0 as a plugin.)
1) Missing opening or closing php tags around the code of the plugin.
2) Blank white space before or after the opening/closing php tags of the plugin.
3) Didn’t remove the code from functions.php before implementing it in the plugin.
4) Duplicate function conflict with another function (not likely since it works out of functions.php). See #3 above. First disable all other plugins and see if the problem persists. If the problem goes away, then rename the email functions and the filter hooks that call them. For example: rename ‘new_mail_from’ to: mycool_new_email_from’.
You can wrap functions like this:
( ! function_exists( 'your_function_name' ) ) { your function }
In order to make sure the function is not duplicated, however this will cause your customized functions to simply not load, and thus not change the email message output.Hope that helps,
Best,
Aaron
Awesome, is it possible though to implement HTML instead of just a custom message? WOuld like to out in company logo with some standard HTML/CSS styling?
Hi Shaun,
I haven’t tries this but you should be able to add whatever you want into $message, for example:
worked perfectly, thanks!
I just want to say a great THANK YOU!
You cannot imagine how many solutions implement & only yours successfully worked!
I just have one problem :
I want to insert whole text inside 3 divs and one h2 element.
example :
To activate your new account, please click the following link:
ACTIVATION LINK
After you activate you will be able to log in.
How can this be implemented?
Thank You!
Oups it didn’t appear divs and h2 element.
I want do this :
div div div h2
To activate your new account, please click the following link:
ACTIVATION LINK
After you activate you will be able to log in.
h2 div div div div
Thank You!
Hi Naser,
I haven’t tested this, but I think for WordPress to parse html in your message you may need to set wp_mail_content_type to text/html by adding the following snippet:
add_filter ("wp_mail_content_type", "set_my_mail_content_type");
function set_my_mail_content_type() {
return "text/html";
}
THANK YOU for this!!!!
Hi Aaron,
It works almost.
From e-mail and name are working, but the subject and the body are still the same.
Could you please help me out?
I’ve created a plugin with this code:
– – –
<?php
/**
* Plugin Name: Custom Activation Emails
* Description: Deze plugin wijzigt de standaard activatie e-mail.
*/
//First we'll setup the from name and sender email.
//Here is the filter for the from name:
add_filter('wp_mail_from', 'new_mail_from');
//Here is the filter for the sender name:
add_filter('wp_mail_from_name', 'new_mail_from_name');
//Here is where the new from email goes:
function new_mail_from($old) {
return '—————-';
}
//Here is where the new sender name goes:
function new_mail_from_name($old) {
return 'Vereniging voor Credit Management | Website';
}
//Next we'll hook into the subject and setup a function to change it:
add_filter( 'wpmu_signup_user_notification_subject', 'my_activation_subject', 10, 4 );
function my_activation_subject( $text ) {
//Here is where to input the new subject for the activation email:
return '——— Credit Management | Lid worden';
}
// Finally we hook into the email itself and run a function to modify the message.
add_filter('wpmu_signup_user_notification_email', 'my_custom_email_message', 10, 4);
function my_custom_email_message($message, $user, $user_email, $key) {
//Here is the new message:
$message = sprintf(__(( "Hartelijk dank voor uw aanmelding. Om uw account te activeren, dient u nog wel op deze URL te klikken:\n\n%s\n\n Na deze activatie kunt u inloggen en o.a. uw profiel bekijken.\n\n" ),
$user, $user_email, $key, $meta),site_url( "?page=gf_activation&key=$key" ));
return sprintf($message);
}
– – –
Thanks!
Hi Amar,
I copied your plugin exactly into my installation and it worked as expected, including the Subject and Message. All I can advise is to disable other plugins and see if something is causing a conflict.
Best Regards,
Aaron
If you want to access information about the signup you can var_dump the user’s information based on the
$key using the GFSignup class.
“`
//var_dump the $signup object to a file to view (warning it’s about 10,500 lines long)
if (class_exists(‘GFSignup’)){
$signup = GFSignup::get( $key );
ob_start();
var_dump($signup);
$thisuser = ob_get_clean();
file_put_contents(‘gf_user_dump.txt’,$thisuser);
}
//example, if I want to get the role the user signed up for
$thisuser->config[“meta”][“role”]
“`
Hope this helps others
Thanks Andrew
@Andrew – Or you could do this:
$signup = GFSignup::get($key);
$userMeta=rgars($signup->config,’meta’);
// then
$role= $userMeta[‘role’];
//If you want to access any custom user meta you’ve mapped to your user registration feed, it,s a little more complicated:
$lead=$signup->lead;
$customMeta=array_column(array_intersect_key($userMeta,array_keys(array_column($userMeta,’key’),’gf_custom’)),’custom_key’,’value’);
$meta=array_combine($customMeta,array_intersect_key($lead,$customMeta));
// then you can get whatever custom user meta data like so:
$my_gf_custom_user_meta=$meta[‘my_gf_custom_user_meta’];
I hope this helps …
Hi…I have been pulling out my hair trying to figure out what is wrong with the pluggin I created based on your blog. The changing of the e-mail address works when I modify it but the subject and message parts will not change or work. Am I missing something in the code?
This is likely due to a conflict with another plugin. Try and deactivate all your other plugins. If this fixes the issue, then try renaming the functions in the email plugin, or change the priority call on the function.
Hi
Above code not working pls advice me, above code could you provide plugin
Hi, I would need some more information about your how you are using the code to troubleshoot.
Great sharing! Only thing to change (in my case) is this line of code a the bottom:
$user, $user_email, $key, $meta),site_url( “?page=gf_activation&key=$key” ));
to
$user, $user_email, $key, $meta),site_url( “wp-activate.php?key=$key” ));
Thanks! That is great for people who aren’t using Gravity Forms to trigger the activation.
Thanks for this code buddy. But still need some help
Actually i want to send a custom message as per the user role say for example “supplier” which is working fine as per the code. but for other user like admin, subscriber, authors etc i only want to send the default wordpress activation email. Can you help to sort this condition.
Hello Kamlesh,
There is a bit more to know about your situation in order to create a solution. How are you registering users? I believe the user registration email goes out before their account has been created and confirmed with a user role in the database. So they may not have a role yet. I’m thinking you would need to filter the register_post, user_register or user_registration_email to check what role they are intended to be created at and then conditionally change the emails from there.
Hi,
thanks a lot, I have been able to customize subject in Activation email. After that, WordPress sends another email with subject Account activated. Can you advise me how to change subject of this email as well?
Thanks!
Zora
I think you need to use the wp_new_user_notification_email filter. (not tested)
add_filter( 'wp_new_user_notification_email', 'new_user_notification_email' );
function new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {
$wp_new_user_notification_email['subject'] = sprintf( '[%s] New user %s registered.', $blogname, $user->user_login ); //edit subject here
$wp_new_user_notification_email['message'] = sprintf( "%s ( %s ) has registerd to your blog %s.", $user->user_login, $user->user_email, $blogname ); //edit message here
return $wp_new_user_notification_email;
}
Hey,
thanks for reply.
Yeah, I have been playing around with “wp-new-user-notification_email” code and tried many alternatives, but nothing works. I have tried your suggestion, but it also won’t do the job.
Just for clarity, did I edit subject correctly or do I miss something there?
add_filter( ‘wp_new_user_notification_email’, ‘new_user_notification_email’ );
function new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {
$wp_new_user_notification_email[‘subject’] = sprintf( ‘[%s] New user %s registered.’, $blogname, $user->user_login ); ‘Account activated’;
return $wp_new_user_notification_email;
}
Hi,
Your edit is in the wrong place:
$wp_new_user_notification_email[‘subject’] = sprintf( ‘[%s] CUSTOM TEXT HERE:’, $blogname, $user->user_login );
Hi there,
Hope you’re still around to see this.
This partly works but not sure what else the problem could be.
Firstly I put this into a mu-plugin.
I’m also using forminator registration form which also obviously send this default email.
Their key code when it comes as default looks like “mysite.com/?page=account_activation&key=901f29b0c34aa4b0” so I changed the “?page=gf_activation&key=$key” to “?page=account_activation&key=”
I’ve also tried “?page=forminator_activation&key=$key” as I’ve seen in another post elsewhere.
The problem is the message isn’t changing. Although mine doesn’t have the “After you activate, you will receive *another email* with your login.” line in it.
The email address, sender name and subject all change correctly. It’s just the message not changing.
I have ultimate member and better notifications plugins so tried with those disabled and still nothing.
Any thoughts? Thanks.
I just found this: https://docs.hookmax.com/plugin/forminator/1.16.2/class/Forminator_CForm_User_Signups/#
It has a couple of other things in there which could be the key. I don’t really want to go messing around with it not knowing what I’m doing so do you maybe see a way to make it work with forminator?
Thanks.
Hi Simon,
I only use Gravity Forms so can’t really help you with Forminator. It could be that Forminator is already editing that message so you could try different priorities on the hook. The fact that the email, name and subject do work make me think it has something to do with Forminator.