1

We're trying to customize the default e-mail new users receive when registering on our website. For this, we are using the wp_new_user_notification_email filter added in WP4.9.0

For our functions.php file not to be cluttered we have the template for the email in "templates/email_welcome.php". We need to load this file to build the $message variable but the content returned is always empty.

Here's what we have added in our child-theme functions.php

// =================================================
// Set custom new user e-mail notification template
// =================================================
add_filter( 'wp_new_user_notification_email', 'custom_wp_new_user_notification_email', 999, 3 );

function custom_wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {
    global $wpdb;

    $userEmail  = stripslashes( $user->user_email );
    $user_login = $user->user_login;
    $siteUrl    = get_site_url();
    $logoUrl    = get_stylesheet_directory_uri() . 'android-chrome-192x192.png';
    $key = $wpdb->get_var($wpdb->prepare("SELECT user_activation_key FROM $wpdb->users WHERE user_login = %s", $user_login));
    if ( empty($key) ) {
        // Generate something random for a key...
        $key = wp_generate_password(20, false);
        do_action('retrieve_password_key', $user_login, $key);
        // Now insert the new md5 key into the db
        $wpdb->update($wpdb->users, array('user_activation_key' => $key), array('user_login' => $user_login));
    }
    $rplink     = '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), 'login') . ">\r\n";

    ob_start();
    include get_stylesheet_directory_uri() . '/templates/email_welcome.php';
    $message = ob_get_clean();

    $wp_new_user_notification_email['subject'] = sprintf( 'Welcome to Site.com' );
    $wp_new_user_notification_email['message'] = $message;
    $wp_new_user_notification_email['headers'] = 'Content-Type: text/html';

    return $wp_new_user_notification_email;
}

Unfortunately this doesn't work. We also tried to reference a function instead..

function custom_wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {
    $message = wpse5427_new_user_custom_content_html($user);

    $wp_new_user_notification_email['subject'] = sprintf( 'Welcome to Site.com' );
    $wp_new_user_notification_email['message'] = $message;
    $wp_new_user_notification_email['headers'] = 'Content-Type: text/html';

    return $wp_new_user_notification_email;
}

function wpse5427_new_user_custom_content_html($user) {
    global $wpdb;
    $userEmail  = stripslashes( $user->user_email );
    $user_login = $user->user_login;
    $siteUrl    = get_site_url();
    $logoUrl    = get_stylesheet_directory_uri() . 'android-chrome-192x192.png';
    $key = $wpdb->get_var($wpdb->prepare("SELECT user_activation_key FROM $wpdb->users WHERE user_login = %s", $user_login));
    if ( empty($key) ) {
        // Generate something random for a key...
        $key = wp_generate_password(20, false);
        do_action('retrieve_password_key', $user_login, $key);
        // Now insert the new md5 key into the db
        $wpdb->update($wpdb->users, array('user_activation_key' => $key), array('user_login' => $user_login));
    }
    $rplink     = '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), 'login') . ">\r\n";

    ob_start();
    include get_stylesheet_directory_uri() . '/templates/email_welcome.php';
    $message = ob_get_clean();

    return $message;
}

But this doesn't work either. Is it possible at all to include an external file in a filter like that?

Here's the content for email_welcome.php:

<?php
// =================================================
// Custom new user notification template
// =================================================
defined( 'ABSPATH' ) || exit;
?>
<style type="text/css">
body {
  background-color: #f2f2f2;
  font-family:"Open sans",sans-serif;
}

h1 {
    color:#111;
    font-family:"Raleway",sans-serif;
    font-weight:700;
    margin-top:0;
    text-align:center;
}

.main-content-wrapper{
    max-width:800px;
    margin:50px auto;
    background:#fff;
    border:#333 3px solid;
    border-radius:5px;
    text-align:center;
}

.site-logo{
    text-align:center;
    background:#333;
    padding:10px;
}

.site-logo img{
}

.main-content{
    padding:30px 50px;
}

.email-footer {
    max-width:800px;
    margin:0 auto;
    text-align:center;
    color:#bbb;
    font-size:80%;
}

.email-button {
    display:inline-block;
    background:#ea640f;
    text-transform:uppercase;
    text-decoration:none;
    padding:15px 30px;
    margin:20px auto 30px;
    color:#fff;
    font-weight:bold;
    border:#ea640f 2px solid;
    border-radius:3px;
}

</style>

<div class="main-content-wrapper">
    <div class="site-logo">
    <img src="<?php echo $logoUrl; ?>" alt="Site.com"/>
    </div>
    <div class="main-content">
<h1>Welcome to Site</h1>
    <p style="margin-top:30px;">
    <?php if ( $user->first_name != '' ) : ?>
        Thank you for joining us <?php echo $user->first_name; ?>.
        </p>
        <p>
        You're only steps away from completing your account creation!
        </p>
    <?php else : ?>
        You're only steps away from completing your account creation!
        </p>
    <?php endif; ?>
    <p>
        Please reset your password using the button below
    </p>
        <a href="<?php echo $rplink; ?>" class="email-button">Reset Password</a>
        <br>
        <strong>Sincerely,</strong><br>
        The Site Team
    </p>
    </div>
</div>
<div class="email-footer">
© 2019 Site.com – All Rights Reserved.
</div>

Thank you!


EDIT: For those of you looking to do the same thing, here's a working code:

// =================================================
// Set custom new user e-mail notification template
// =================================================
add_filter( 'wp_new_user_notification_email', 'custom_wp_new_user_notification_email', 999, 3 );

function custom_wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {   
    global $wpdb;

    $userEmail  = stripslashes( $user->user_email );
    $user_login = $user->user_login;
    $siteUrl    = get_site_url();
    $logoUrl    = get_stylesheet_directory_uri() . '/android-chrome-192x192.png';

    // Generate something random for a key...
    $key = wp_generate_password(20, false);
    do_action('retrieve_password_key', $user_login, $key);
    // Now insert the key, hashed, into the DB.
    if ( empty( $wp_hasher ) ) {
            require_once ABSPATH . WPINC . '/class-phpass.php';
            $wp_hasher = new PasswordHash( 8, true );
    }
    $hashed = time() . ':' . $wp_hasher->HashPassword( $key );
    $wpdb->update($wpdb->users, array('user_activation_key' => $hashed ), array( 'user_login' => $user->user_login ) );

    $rplink     = network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), 'login');

    ob_start();
    include get_stylesheet_directory() . '/templates/email_welcome.php';
    $message = ob_get_clean();

    $wp_new_user_notification_email['subject'] = sprintf( 'Welcome to Site.com' );
    $wp_new_user_notification_email['message'] = $message;
    $wp_new_user_notification_email['headers'] = 'Content-Type: text/html';

    return $wp_new_user_notification_email;
}

Here's the update template for email_welcome.php:

<?php
// =================================================
// Custom new user notification template
// =================================================
defined( 'ABSPATH' ) || exit;
?>
<div style='font-family:"Open sans",sans-serif;background-color: #f2f2f2;padding:50px;'>
    <div style="max-width:800px;margin:0 auto;background:#fff;border:#333 3px solid;border-radius:5px;text-align:center;">
        <div style="text-align:center;background:#333;padding:5px 5px 2px;">
            <img src="<?php echo $logoUrl; ?>" alt="Site.com" style="height:45px;">
        </div>
        <div style="padding:30px 50px;">
            <h1 style='color:#111;font-family:"Raleway",sans-serif;font-weight:700;margin-top:0;text-align:center;'>Welcome to Site</h1>
            <p style="margin-top:30px;">
            <?php if ( $user->first_name != '' ) : ?>
                Thank you for joining us <?php echo $user->first_name; ?>.
                </p>
                <p>
                You're only steps away from completing your account creation!
                </p>
            <?php else : ?>
                You're only steps away from completing your account creation!
                </p>
            <?php endif; ?>
            <p>
                Please reset your password using the button below
            </p>
                <a href="<?php echo $rplink; ?>" style="display:inline-block;background:#ea640f;text-transform:uppercase;text-decoration:none;padding:15px 30px;margin:20px auto 30px;color:#fff;font-weight:bold;border:#ea640f 2px solid;border-radius:3px;">Reset Password</a>
                <br>
                <strong>Sincerely,</strong><br>
                The Site Team
            </p>
        </div>
    </div>
    <div style="max-width:800px;margin:30px auto 0;text-align:center;color:#bbb;font-size:80%;">
    © 2019 Site.com – All Rights Reserved.
    </div>
</div>

Note 1: If you use any plugin that interacts with the login/registration process, this may not work at all. If you have issues, test with all plugins disabled and re-enable them one by one.

Note 2: The below solution uses an e-mail template email_welcome.php located in the "/templates" folder of your theme.

Note 3: Code was updated to always regenerate $key to allow pretty links and store the hashed $key as it's the case in the WP function.

2

I believe the problem is you're using get_stylesheet_directory_uri() function in an attempt to include a file. This function returns a URL when what you want to do when you need to include a file is provide a path. You could be using instead is get_stylesheet_directory()

Here's what the output would look like:

// - https://yourwebsite.com/wp-content/themes/my-theme/
$url = get_stylesheet_directory_uri();

// - \www\httpdocs\wp-content\themes\my-theme\
$directory = get_stylesheet_directory();
  • Good catch, it worked like a charm! Our classes got wrecked by Gmail but that's another issue and I kind of expected that. Adding the style directly inline might be a better option. Thanks again for your help! – thisisbbc Jan 31 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.