4

I would like to change the body of the password reset email that WP sends out with something more friendly.

I am trying to use the "retrieve_password_message" filter but I am messing it up.

Can someone kindly post a sample code?

6

the given answers have good intentions. But are not good. There are more parameters to the filter function call, 4 to be exact.

this worked for me for the message (i wanted to replace username with user email)

note we have a multi site page. You might want to replace network_site_url with get_site_url()

typically in your functions.php of your wordpress stheme:

add_filter("retrieve_password_message", "mapp_custom_password_reset", 99, 4);

function mapp_custom_password_reset($message, $key, $user_login, $user_data )    {

  $message = "Someone has requested a password reset for the following account:

" . sprintf(__('%s'), $user_data->user_email) . "

If this was a mistake, just ignore this email and nothing will happen.

To reset your password, visit the following address:

" .  '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . ">\r\n" . "

If you have any further issues, please email us to help@myapp.com

The Myapp Team";


  return $message;

}

bonus: change email title / subject

add_filter( 'retrieve_password_title',
  function( $title )
  {
    $title = __( 'Password reset for myapp.org' );
    return $title;
  }
);
2

I thought that this snippet of code may be helpful for anyone looking to customize the reset password email.

//* Password reset activation E-mail -> Body
add_filter( 'retrieve_password_message', 'wpse_retrieve_password_message', 10, 2 );
function wpse_retrieve_password_message( $message, $key ){
    $user_data = '';
    // If no value is posted, return false
    if( ! isset( $_POST['user_login'] )  ){
            return '';
    }
    // Fetch user information from user_login
    if ( strpos( $_POST['user_login'], '@' ) ) {

        $user_data = get_user_by( 'email', trim( $_POST['user_login'] ) );
    } else {
        $login = trim($_POST['user_login']);
        $user_data = get_user_by('login', $login);
    }
    if( ! $user_data  ){
        return '';
    }
    $user_login = $user_data->user_login;
    $user_email = $user_data->user_email;
    // Setting up message for retrieve password
    $message = "Looks like you want to reset your password!\n\n";
    $message .= "Please click on this link:\n";
    $message .= '<a href="';
    $message .= network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login');
    $message .= '">"';
    $message .= network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login');
    $message .= '"</a>\n\n"';
    $message .= 'Kind Regards,<br/>Dream Team';
    // Return completed message for retrieve password
    return $message;
}
  • 1
    You don't need to check $_POST data to get user_login. You already have user_login in the hook parameter. Use 3 parameter instead of 2. developer.wordpress.org/reference/hooks/… – hkchakladar Nov 5 '17 at 18:28
  • 1
    Sorry for the downvote, but I really wanted to give @Toskan's answer a chance to rise to the top - it is a much better answer and uses the data available from the filter already instead of reconstructing it. – squarecandy May 1 '18 at 19:13
0
add_filter("retrieve_password_message", "your_custom_message", 99, 2);

  function your_custom_message($message, $key)    {

  $message = "your_custom_message";

  return $message;

}

This is an example of usage of 'retrieve_password_message' filter hook.

This is to override the email body which someone gets while retrieving the password.

  • Please note: I have not tested it. – WisdmLabs Sep 6 '14 at 18:36
  • 1
    Please explain what it does. – fuxia Sep 6 '14 at 23:10
  • 1
    Hi thanks for the input :-) Could you please add the code for the reset password link. – William Sep 7 '14 at 4:59
  • @toscho, I have updated the answer with explanation. – WisdmLabs Sep 7 '14 at 16:56
  • 1
    @WisdmLabs The password reset link is missing from your snippet. exmple.com/wp-login.php?action=rp&key={ the key } &login={ the username } :-) – William Sep 8 '14 at 9:33

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.