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 ([email protected])

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 [email protected]

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:

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 '[email protected]';
}

//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.

 

Share Aaron Jerad Designs on Facebook

24 Comments

  1. Ian Blackford · July 4, 2014 Reply

    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.

  2. Ian · August 21, 2014 Reply

    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

    • ajdesigns · August 25, 2014 Reply

      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

  3. David Kuhn · September 23, 2014 Reply

    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

    • Aaron Jerad · September 23, 2014 Reply

      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

  4. Shaun · June 20, 2015 Reply

    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?

    • Aaron Jerad · June 20, 2015 Reply

      Hi Shaun,
      I haven’t tries this but you should be able to add whatever you want into $message, for example:

      
      $logo = ' < img class="my-logo"  src"your-image"> '; 
      
      $message = sprintf(__(($logo . "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" ));
      
      
  5. Paul Spencer · August 6, 2015 Reply

    worked perfectly, thanks!

  6. Naser · August 22, 2015 Reply

    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!

  7. Naser · August 22, 2015 Reply

    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!

    • Aaron Jerad · August 24, 2015 Reply

      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";
      }

  8. Susan · November 15, 2015 Reply

    THANK YOU for this!!!!

  9. Amar · December 13, 2015 Reply

    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!

    • Aaron Jerad · December 13, 2015 Reply

      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

  10. Andrew McCormick · January 27, 2016 Reply

    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

    • Aaron Jerad · January 27, 2016 Reply

      Thanks Andrew

    • Manu Fruteau · February 24, 2016 Reply

      @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 …

  11. Michael Cofelice · February 8, 2016 Reply

    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?

    • Aaron Jerad · February 8, 2016 Reply

      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.

  12. siva · February 18, 2016 Reply

    Hi
    Above code not working pls advice me, above code could you provide plugin

    • Aaron Jerad · February 18, 2016 Reply

      Hi, I would need some more information about your how you are using the code to troubleshoot.

  13. David · December 27, 2016 Reply

    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” ));

    • Aaron Jerad · December 27, 2016 Reply

      Thanks! That is great for people who aren’t using Gravity Forms to trigger the activation.

Leave a Reply