When a new user signs up, the confirmation email is received and this is what it says:

Username: testuser To set your password, visit the following address:  

But I want to customize it, so I added the following code to the functions.php
but it does not work. The default email is still sent.

I am using WordPress 4.9.9

add_filter( 'wp_new_user_notification_email', 'custom_wp_new_user_notification_email', 10, 3 );
function custom_wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {
$wp_new_user_notification_email['subject'] = sprintf(__( '[%s] Your username and password' ), $blogname, $user->user_login );
$key = get_password_reset_key( $user );
$message = sprintf(__('Welcome to our website,')) . "\r\n\r\n";
$message .= 'To set your password, visit the following address:' . "\r\n\r\n";
$message .= network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), 'login') . "\r\n\r\n";
$message .= "After this you can enjoy our website!" . "\r\n\r\n";
$message .= "Kind regards," . "\r\n";
$message .= "Support Team" . "\r\n";
$wp_new_user_notification_email['message'] = $message;
$wp_new_user_notification_email['headers'] = 'From: MyName<example@domain.ext>'; 
// this just changes the sender name and email to whatever you want (instead of the default WordPress <wordpress@domain.ext>
return $wp_new_user_notification_email;

Why doesn't the code work? Would you help me?

  • @vikrant zilpe my friend i need the set password link. my users need to set their password
    – Sh.Dehnavi
    Dec 22 '18 at 9:50
  • @vikrant zilpe not reset password. after sing up in wordpress sites, it is received a mail to set your password
    – Sh.Dehnavi
    Dec 22 '18 at 9:55
  • please try this code : $message . = '<a href="' . network_site_url("wp-login.php?action=rp&key=$adt_rp_key&login=" . rawurlencode($user_login), 'login') . '">' . network_site_url("wp-login.php?action=rp&key=$adt_rp_key&login=" . rawurlencode($user_login), 'login') . '</a>' Dec 22 '18 at 10:16
  • @vikrant it does not wrok :|
    – Sh.Dehnavi
    Dec 22 '18 at 11:17
  • what s problem ? Dec 22 '18 at 11:21

There is actually nothing wrong with the code that you have (I tested it as you have it above and it works fine).

But you have to account for some additional possibilities:

  1. The wp_new_user_notification() function is a pluggable function. So it can be replaced by a plugin or by custom code. And if it is replaced, there is a possibility that the custom version of the function lacks the wp_new_user_notification_email filter hook.
  2. Possibly another filter already exists hooked to this same filter, but at a earlier priority (<10) and it redirects or otherwise exits the process. That would be bad form, but it is possible. This isn't all that likely the problem, but it is possible so that's why I mention it.
  3. The possibility exists you are targeting the wrong function. We're assuming that the email is the default WP registration, but you didn't state that specifically. If you're doing something where there's a custom registration and/or some other process, it doesn't necessarily go through wp_new_user_notification().

With those possibilities, I would recommend you do the following:

  1. Review your theme to make sure this is the only instance of a wp_new_user_notification_email filter.
  2. Disable ALL plugins.
  3. Re-test with all plugins deactivated.
  • i have a plugin (name: better notification) for sending and customizing default wp emails. do you think, it is because of that? that plugin is active but i am not using that for WP email registration
    – Sh.Dehnavi
    Dec 30 '18 at 6:12
  • It doesn't matter whether you're using that for WP email registration or not - it probably has some filters that hook into the process - OR worse, it uses pluggable functions that end up removing the native WP filters. That's why I recommend you disable plugins and retest. Did you do that?
    – butlerblog
    Dec 30 '18 at 12:57
  • A quick look at the "better notification" plugin indicates that it does use wp_new_user_notification() as a pluggable function - and guess what filter it doesn't have in the plugged function? Now, that's just perusing their code - you need to follow my original directions and deactivate to test to determine if that's actually your problem (which seems even more likely now).
    – butlerblog
    Dec 30 '18 at 13:09

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.