Last updated on September 25, 2023

Customize the WordPress Password Reset Email

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Personalize reset email content.

The code snippet below demonstrates how to customize the WordPress password reset email. This can be useful when you want to personalize the email content or design to match your website’s branding or add additional information for your users.

/**
 * Customize the WordPress password reset email.
 */
function wpsnippets_customize_password_reset_email( $message, $key, $user_login, $user_data ) {
    // Get the password reset URL
    $reset_url = network_site_url( "wp-login.php?action=rp&key=$key&login=" . rawurlencode( $user_login ), 'login' );

    // Customize the email subject
    $subject = 'Password Reset Request';

    // Customize the email message
    $message = "Hi " . $user_data->display_name . ",nn";
    $message .= "You have requested to reset your password for your account on " . get_bloginfo( 'name' ) . ".nn";
    $message .= "Please click on the following link to reset your password:nn";
    $message .= $reset_url . "nn";
    $message .= "If you did not request a password reset, please ignore this email.nn";
    $message .= "Thanks,n";
    $message .= get_bloginfo( 'name' );

    // Return the customized email
    return $message;
}
add_filter( 'retrieve_password_message', 'wpsnippets_customize_password_reset_email', 10, 4 );

In this code snippet, we are using the retrieve_password_message filter to customize the password reset email. The wpsnippets_customize_password_reset_email function accepts four parameters: $message (the default email message), $key (the password reset key), $user_login (the user’s login username), and $user_data (the user’s data object).

Inside the function, we first generate the password reset URL using the network_site_url function. Then, we customize the email subject and message according to our needs. In this example, we include the user’s display name, the website’s name, and the password reset URL in the email message.

Finally, we return the customized email message to override the default WordPress password reset email.

Remember to replace the email content and subject with your own customized message.

Examples

Example 1: Customize the WordPress Password Reset Email

This use case demonstrates how to customize the content and styling of the WordPress password reset email.

function wpsnippets_custom_password_reset_email( $message, $key, $user_login, $user_data ) {
    // Customize the email subject
    $subject = 'Custom Password Reset';

    // Customize the email message
    $message = 'Dear ' . $user_data->display_name . ',<br><br>';
    $message .= 'You have requested to reset your password. Please click the following link to reset it: <br><br>';
    $message .= '<a href="' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . '">Reset Password</a><br><br>';
    $message .= 'If you did not request a password reset, please ignore this email.<br><br>';
    $message .= 'Regards,<br>';
    $message .= 'Your Website Team';

    // Return the customized email
    return $message;
}
add_filter( 'retrieve_password_message', 'wpsnippets_custom_password_reset_email', 10, 4 );

This code example demonstrates how to customize the content of the WordPress password reset email. The wpsnippets_custom_password_reset_email function is hooked into the retrieve_password_message filter and accepts the default email message, reset key, user login, and user data as parameters. It then customizes the email subject and message using the provided information and returns the modified email message.

Example 2: Customize the WordPress Password Reset Email Subject

This use case demonstrates how to customize only the subject of the WordPress password reset email.

function wpsnippets_custom_password_reset_email_subject( $subject ) {
    // Customize the email subject
    $subject = 'Custom Password Reset';

    // Return the customized email subject
    return $subject;
}
add_filter( 'retrieve_password_title', 'wpsnippets_custom_password_reset_email_subject' );

This code example demonstrates how to customize only the subject of the WordPress password reset email. The wpsnippets_custom_password_reset_email_subject function is hooked into the retrieve_password_title filter and accepts the default email subject as a parameter. It then customizes the email subject and returns the modified subject.

Example 3: Customize the WordPress Password Reset Email Header and Footer

This use case demonstrates how to customize the header and footer of the WordPress password reset email.

function wpsnippets_custom_password_reset_email_header() {
    // Output custom email header
    echo '<div style="background-color: #f2f2f2; padding: 20px;">';
    echo '<h1 style="color: #333;">Custom Password Reset</h1>';
    echo '</div>';
}

function wpsnippets_custom_password_reset_email_footer() {
    // Output custom email footer
    echo '<div style="background-color: #f2f2f2; padding: 20px;">';
    echo '<p style="color: #333;">Regards,<br>Your Website Team</p>';
    echo '</div>';
}

add_action( 'retrieve_password', 'wpsnippets_custom_password_reset_email_header' );
add_action( 'retrieve_password', 'wpsnippets_custom_password_reset_email_footer' );

This code example demonstrates how to customize the header and footer of the WordPress password reset email. The wpsnippets_custom_password_reset_email_header and wpsnippets_custom_password_reset_email_footer functions are hooked into the retrieve_password action. They output the custom email header and footer HTML respectively. By adding these actions, the custom header and footer will be included in the password reset email.

Last updated on September 25, 2023. Originally posted on October 5, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *