What's wrong with Customizing new user notification email by add_filter?

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

?php
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=rpkey=$keylogin= . 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: [email protected]'; 
// this just changes the sender name and email to whatever you want (instead of the default WordPress [email protected]
        
return $wp_new_user_notification_email;
}
?

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

Topic email-verification email user-registration customization Wordpress

Category Web


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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.