Home » Wordpress » Override retrieve_password(); function in wordpress with custom one

Override retrieve_password(); function in wordpress with custom one

Posted by: admin November 30, 2017 Leave a comment


I want to send a custom password retrival email from wordpress, one where i can style it so it looks less ugly.

I see that wordpress uses a function to do this but i have no idea how to over

function retrieve_password($user_email) {
        global $wpdb, $current_site;

        $errors = new WP_Error();

        // redefining user_login ensures we return the right case in the email
        $user_login = $user_email;

        do_action('retreive_password', $user_login);  // Misspelled and deprecated
        do_action('retrieve_password', $user_login);

        $key = $wpdb->get_var($wpdb->prepare("SELECT user_activation_key FROM $wpdb->users WHERE user_login = %s", $user_login));
        if ( empty($key) ) {
                // Generate something random for a key...
                $key = wp_generate_password(20, false);
                do_action('retrieve_password_key', $user_login, $key);
                // Now insert the new md5 key into the db
                $wpdb->update($wpdb->users, array('user_activation_key' => $key), array('user_login' => $user_login));
        $message = __('Someone requested that the password be reset for the following account:') . "\r\n\r\n";
        $message .= network_site_url() . "\r\n\r\n";
        $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
        $message .= __('If this was a mistake, just ignore this email and nothing will happen.') . "\r\n\r\n";
        $message .= __('To reset your password, visit the following address:') . "\r\n\r\n";
        $message .= '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . ">\r\n";

        if ( is_multisite() )
                $blogname = $GLOBALS['current_site']->site_name;
                // 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);

        $title = sprintf( __('[%s] Password Reset'), $blogname );

        $title = apply_filters('retrieve_password_title', $title);
        $message = apply_filters('retrieve_password_message', $message, $key);

        if ( $message && !wp_mail($user_email, $title, $message) )
                wp_die( __('The e-mail could not be sent.') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function...') );

        return true;

Does anyone know how to make this function run instead of the one found in wp_login.php



Just override the content. You’ll see 2 lines that are of particular interest.

$title = apply_filters('retrieve_password_title', $title);
$message = apply_filters('retrieve_password_message', $message, $key);

This allows you to replace the content of the title and message that has been generated. Here’s an example.

function wpse_custom_retrieve_password_message( $message, $key ) {
    return "Your custom email content";
add_filter( 'retrieve_password_message', 'wpse_custom_retrieve_password_message', 10, 2 );