Created
December 9, 2020 09:24
-
-
Save meetawahab/825370259a3501ffeb41c1f008266a3d to your computer and use it in GitHub Desktop.
Fix for space issue in username during WordPress reset password email link with the LoginPress - Hide Login Add-On.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function loginpress_reset_password_email( $message ) { | |
$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 ''; | |
} | |
$key = get_password_reset_key( $user_data ); | |
if ( is_wp_error( $key ) ) { | |
return $key; | |
} | |
if ( is_multisite() ) { | |
$site_name = get_network()->site_name; | |
} else { | |
/* | |
* 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. | |
*/ | |
$site_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); | |
} | |
// Redefining user_login ensures we return the right case in the email. | |
$user_login = $user_data->user_login; | |
// Setting up message for retrieve password | |
$message = __( 'Someone has requested a password reset for the following account:' ) . "\r\n\r\n"; | |
/ translators: %s: Site name. / | |
$message .= sprintf( __( 'Site Name: %s' ), $site_name ) . "\r\n\r\n"; | |
/ translators: %s: User login. / | |
$message .= sprintf( __( 'Username: %s' ), $user_login ) . "\r\n\r\n"; | |
//adding %20 for spaces in found in username | |
$user_login = str_replace( ' ', '%20', $user_login ); | |
$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'); | |
// Return modified complete message for retrieve password | |
return $message; | |
} | |
// If LoginPress - Hide Login is activated. | |
if( class_exists( 'LoginPress_HideLogin_Main' ) ) { | |
// Apply the reset_password_email() function on the "retrieve password" message: | |
add_filter( 'retrieve_password_message', 'loginpress_reset_password_email' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment