Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save faisalahammad/e8aea3ca4549f593a0f430793680fa7a to your computer and use it in GitHub Desktop.
Save faisalahammad/e8aea3ca4549f593a0f430793680fa7a to your computer and use it in GitHub Desktop.
Enable WooCommerce users to log in using either their phone number or email address. This code snippet overrides the default WooCommerce login logic, allowing for flexible login options and improving the user experience on the account login page.
<?php
/**
* Woocommerce Add Phone Number To Login Account
* @author Faisal Ahammad
*/
// Enable login with phone number or email
add_filter( 'authenticate', 'login_with_phone_or_email', 20, 3 );
/**
* @param $user
* @param $username
* @param $password
* @return mixed
*/
function login_with_phone_or_email( $user, $username, $password )
{
if ( empty( $username ) ) {
return $user;
}
if ( is_email( $username ) ) {
$user = get_user_by( 'email', $username );
} else {
$user_query = new WP_User_Query( array(
'meta_key' => 'billing_phone',
'meta_value' => $username,
'number' => 1,
) );
$users = $user_query->get_results();
if ( !empty( $users ) ) {
$user = $users[ 0 ];
}
}
if ( $user && wp_check_password( $password, $user->user_pass, $user->ID ) ) {
return $user;
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment