Create a Custom “Lost Password” Email in WordPress :
Add this code in functions.php :
We’ve all done it. Not being able to
remember passwords is one of the most annoying things about having so many
website accounts across the internet. Fortunately, browsers nowadays let you
save usernames and passwords for most websites so you don’t have to worry about
losing login information that much. However, it does still happen. If you allow
user registration on your WordPress site, users will inevitably need to reset
their password at some point.
WordPress provides a default
password-reset process that involves the user entering their username or email
into a form. Once the form is submitted, WordPress sends an email to the user
with that username/password that contains a link to reset their password. Once
reset, the user can log in using their new password. The thing is that the
canned email for this purpose is rather plain. The bright side, though, is
that, like most things, WordPress gives us a way to customize this email. The
code below does the following:
- Changes the “From” email address. This would be the email any reply is sent to.
- Changes the “From” name. This shows the recipient who the email is from.
- Changes the email subject.
- Changes the content type of the email to HTML so we can use markup within the message. Before, it was a plain-text message.
- Creates a custom message to use in the email body. This process involves retrieving the user’s data based on their email/username, assembling a URL with appropriate query arguments so WordPress can do the password reset, and adding a custom message with the link to reset the password. The only part you’ll change is the message at the bottom; almost everything else needs to remain the same to work. Just make sure to include the link that lets the user actually reset the password.
Add this code in functions.php :
-
<?php
//* Change "From" email address
add_filter( 'wp_mail_from', function( $email ) {
return 'hello@mydomain.com'; });
//* Change "From" email name
add_filter( 'wp_mail_from_name', function( $name ) {
return 'My Website'; });
//* Change Subject
add_filter( 'retrieve_password_title', function() {
return 'Password Recovery'; });
//* Change email type to HTML
add_filter( 'wp_mail_content_type', function( $content_type ) {
return 'text/html'; });
//* Change the message/body of the email
add_filter( 'retrieve_password_message', 'rv_new_retrieve_password_message', 10, 2 );
function rv_new_retrieve_password_message( $message, $key ){
// Bail if username or email is not entered
if ( ! isset( $_POST['user_login'] ) )
return;
// Get user's data
if ( strpos( $_POST['user_login'], '@' ) ) { # by email
$user_data = get_user_by( 'email', trim( $_POST['user_login'] ) );
} else { # by username
$login = trim( $_POST['user_login'] );
$user_data = get_user_by( 'login', $login );
}
// Store some info about the user
$user_fname = $user_data->user_firstname;
$user_login = $user_data->user_login;
$user_email = $user_data->user_email;
// Assembled the URL for resetting the password
$reset_url = add_query_arg(array(
'action' => 'rp',
'key' => $key,
'login' => rawurlencode( $user_login )
), wp_login_url() );
// Create and return the message
$message = sprintf( '<h2>%s</h2>', __( 'Hi ' ) . $user_fname );
$message .= sprintf( '<p>%s</p>', __( 'It looks like you need to reset your password on the site. If this is correct, simply click the link below. If you were not the one responsible for this request, ignore this email and nothing will happen.' ) );
$message .= sprintf( '<p><a href="%s">%s</a></p>', $reset_url, __( 'Reset Your Password' ) );
return $message; }
?>
No comments:
Post a Comment