diff --git a/includes/class-wc-form-handler.php b/includes/class-wc-form-handler.php
index 78436322375..8bf8b6efaba 100644
--- a/includes/class-wc-form-handler.php
+++ b/includes/class-wc-form-handler.php
@@ -766,7 +766,8 @@ class WC_Form_Handler {
if ( ! empty( $_POST['login'] ) && ! empty( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-login' ) ) {
try {
- $creds = array();
+ $creds = array();
+ $username = trim( $_POST['username'] );
$validation_error = new WP_Error();
$validation_error = apply_filters( 'woocommerce_process_login_errors', $validation_error, $_POST['username'], $_POST['password'] );
@@ -775,7 +776,7 @@ class WC_Form_Handler {
throw new Exception( '' . __( 'Error', 'woocommerce' ) . ': ' . $validation_error->get_error_message() );
}
- if ( empty( $_POST['username'] ) ) {
+ if ( empty( $username ) ) {
throw new Exception( '' . __( 'Error', 'woocommerce' ) . ': ' . __( 'Username is required.', 'woocommerce' ) );
}
@@ -783,17 +784,17 @@ class WC_Form_Handler {
throw new Exception( '' . __( 'Error', 'woocommerce' ) . ': ' . __( 'Password is required.', 'woocommerce' ) );
}
- if ( is_email( $_POST['username'] ) && apply_filters( 'woocommerce_get_username_from_email', true ) ) {
- $user = get_user_by( 'email', $_POST['username'] );
+ if ( is_email( $username ) && apply_filters( 'woocommerce_get_username_from_email', true ) ) {
+ $user = get_user_by( 'email', $username );
if ( isset( $user->user_login ) ) {
- $creds['user_login'] = $user->user_login;
+ $creds['user_login'] = $user->user_login;
} else {
throw new Exception( '' . __( 'Error', 'woocommerce' ) . ': ' . __( 'A user could not be found with this email address.', 'woocommerce' ) );
}
} else {
- $creds['user_login'] = $_POST['username'];
+ $creds['user_login'] = $username;
}
$creds['user_password'] = $_POST['password'];
@@ -803,7 +804,7 @@ class WC_Form_Handler {
if ( is_wp_error( $user ) ) {
$message = $user->get_error_message();
- $message = str_replace( '' . esc_html( $creds['user_login'] ) . '', '' . esc_html( $_POST['username'] ) . '', $message );
+ $message = str_replace( '' . esc_html( $creds['user_login'] ) . '', '' . esc_html( $username ) . '', $message );
throw new Exception( $message );
} else {