1

I am modifying the WP registration page to allow some additional information to be captured. As well as capturing this new info I have also modified it to allow users to set their own password.

Everything works great and the user created password is sent to them in an email and can be used to access the website. However this is immediately followed by a second email with a system generated password. The system generated password does not actually work, and I don't want it to, but I'd like to stop the email being sent.

I followed the guide here to get this far.

Thanks.

add_action('user_register', 'copper_user_register', 10, 1);
function copper_user_register($user_id){

    // Save the password
    $userdata = array();

    $userdata['ID'] = $user_id;
    if ( $_POST['password'] !== '' ) {
        $userdata['user_pass'] = $_POST['password'];
    }

    $new_user_id = wp_update_user( $userdata );
    $plaintext_pass =   $_POST['password'];
    wp_new_user_notification( $user_id, $plaintext_pass );

}
1

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.

  • 1
    Thanks for the response! I went with your first option and used the pluggable function. After doing that I didn't see the point in keeping the rest of my code in the functions file so I dumped the whole lot into the plugin and now I can move this piece of functionality between sites with ease. Thanks again. – Gareth Watson Jun 3 '14 at 11:26
  • 1
    Glad to hear it worked for you. Sounds like a good move, taking it into a plugin. I tend to do that myself, if it's non-theme related stuff. – birgire Jun 3 '14 at 11:28

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.