0

I'm trying to customize the email sent to newly registered users. I use the wp_new_user_notification_email filter in a plugin, and it's working fine to set the subject and message. However I would like to send the link to reset the user's password the same way the normal WP notification email does.

From what I see in the wp_new_user_notification function, the password key in hashed before being stored in the database. This key is then used in the url to reset the password. The problem is that I cannot access this variable in the function I call with the wp_new_user_notification_email filter, and if I generate a randow key to put in the url WP throws an error in the form (probably because the new key doesn't correspond to the stored hashed key).

I guess there is a way to achieve this, because having a filter to customize the registration message without giving the link to reset the password would be pretty pointless.

Any idea ?

1

I found a function to retrieve this key : get_password_reset_key().

So now I have this code in my plugin to customize the email sent to newly registered users:

    add_filter('wp_new_user_notification_email', 'change_notification_message', 10, 3);

    function change_notification_message( $wp_new_user_notification_email, $user, $blogname ) {

        // Generate a new key
        $key = get_password_reset_key( $user );

        // Set the subject
        $wp_new_user_notification_email['subject'] = __('Your email subject');

        // Put the username in the message
        $message = sprintf(__('Username: %s'), $user->user_login) . "\r\n\r\n";
        // Give your user the link to reset her password 
        $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 .= wp_login_url() . "\r\n";

        // Set the email's message
        $wp_new_user_notification_email['message'] = $message;

        return $wp_new_user_notification_email;
    }

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.