It looks like you’re calling wp_new_user_notification()
explicitly in your code snippet.
Here are two ideas, how you could get around this problem:
Idea 1:
The wp_new_user_notification()
function is pluggable, so you can modify it to your needs.
You could for example, override it with:
if ( ! function_exists( 'wp_new_user_notification' ) ):
function wp_new_user_notification( $user_id, $plaintext_pass="" )
{
$user = get_userdata( $user_id );
// The blogname option is escaped with esc_html on the way into the database in sanitize_option
// we want to reverse this for the plain text arena of emails.
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$message = sprintf(__('New user registration on your site %s:'), $blogname) . "\r\n\r\n";
$message .= sprintf(__('Username: %s'), $user->user_login) . "\r\n\r\n";
$message .= sprintf(__('E-mail: %s'), $user->user_email) . "\r\n";
@wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message);
}
endif;
where it will only notify the site admin and use this function in your code instead:
function wpse_new_user_notification( $user_id, $plaintext_pass="" )
{
if ( empty($plaintext_pass) )
return;
$user = get_userdata( $user_id );
// The blogname option is escaped with esc_html on the way into the database in sanitize_option
// we want to reverse this for the plain text arena of emails.
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$message = sprintf(__('Username: %s'), $user->user_login) . "\r\n";
$message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
$message .= wp_login_url() . "\r\n";
wp_mail($user->user_email, sprintf(__('[%s] Your username and password'), $blogname), $message);
}
where it will only notify the newly registerd user.
There might be more sophisticated ways to handle this, but this is what comes first to my mind 😉
Idea 2:
You could also try to use the wp_mail
filter and the phpmailer_init
action to terminate duplicate mails if the mail body contains “Your username and password” and the $to
email address isn’t the admin email address.