使用电话号码或正常登录的 Woocommerce 身份验证

问题描述 投票:0回答:1

我有很多问题,我们的客户忘记他们用于登录的电子邮件。除非您进入数据库,否则 WordPress 不会让您更改登录名。我不想将每次登录更改为客户的电话号码。我想编写一些代码,以便在他们登录时为我完成此操作。所以我正在尝试编写代码,让他们能够正常登录,或者他们可以使用他们的电话号码和普通密码。

我从这里的一篇文章中得到了这个想法,但代码在我的网站上给出了一个致命错误。 通过电话号码登录 Woocommerce

这是我到目前为止的代码。我尝试过各种变化。每个都允许电话号码登录,但不允许普通用户/电子邮件。或者,它将允许正常方法但不允许电话号码。我似乎无法让它与两者一起工作。我已经将它放在主题的functions.php文件中。但也许我需要把它放在其他地方?也许代码全部关闭了。

我也不确定为什么这个问题会被降级。我做了很多研究来寻找解决方案。我提到上一篇文章,我想在 WordPress 中使用用户名、电子邮件 ID 和电话号码登录这个不起作用。这里的每个解决方案都不起作用。因此,当这里没有答案时降级我的问题似乎不公平。

/**
 * Allow login with phone number, username, or email
 */
add_filter('authenticate', 'vnmAdmin_loginWithPhoneNumber', 20, 3);
function vnmAdmin_loginWithPhoneNumber($user, $username, $password) {

    // If a user is already authenticated or password is empty, skip
    if (!empty($user) || empty($password)) {
        return $user;
    }

    // Remove non-numeric characters from the username (for phone numbers)
    $normalizedPhone = preg_replace('/\D/', '', $username);

    // If the username is numeric, we treat it as a phone number
    if (is_numeric($normalizedPhone)) {

        // Get users whose billing phone number matches the normalized phone number
        $matchingUsers = get_users(array(
            'meta_key'     => 'billing_phone',
            'meta_value'   => $normalizedPhone,
            'meta_compare' => '='
        ));

        // Check if we have a matching user
        if (!empty($matchingUsers) && is_array($matchingUsers)) {

            // Use the first matching user
            $firstMatchingUser = reset($matchingUsers);

            // Set the username to the matched user's login name
            $username = $firstMatchingUser->user_login;

        } else {
            // If no matching user is found, return an error
            return new WP_Error('invalid_phone', __('No user found with this phone number.'));
        }
    }

    // Proceed with the normal WordPress authentication using the username (either modified for phone or original)
    return wp_authenticate_username_password(null, $username, $password);
}

wordpress authentication woocommerce
1个回答
0
投票

好吧,我自己找到了解决方案。问题是代码返回 null,这会破坏循环。关键是让它检查输入的用户名是否全是数字。所有号码都会触发代码来提取电话号码并将其与用户匹配并允许使用密码登录。 如果它检测到数字以外的任何内容,它不会拦截任何内容并返回客户已输入的用户/电子邮件。

// login with phone number only if the username is all numbers
add_filter('authenticate', 'vnmAdmin_loginWithPhoneNumber', 20, 3);
function vnmAdmin_loginWithPhoneNumber($user, $username, $password) {

    // Check if the input username is entirely numeric (i.e., a phone number)
    if (ctype_digit($username)) {

        // Get users whose billing phone number matches the entered phone number
        $matchingUsers = get_users(array(
            'meta_key'     => 'billing_phone',
            'meta_value'   => $username,
            'meta_compare' => '=' // Exact match
        ));

        // Check if we have a matching user
        if (!empty($matchingUsers) && is_array($matchingUsers)) {

            // Use the first matching user
            $firstMatchingUser = reset($matchingUsers);

            // Set the username to the matched user's login name
            $username = $firstMatchingUser->user_login;
        } else {
            // If no matching user is found, return an error
            return new WP_Error('invalid_phone', __('No user found with this phone number.'));
        }

        // Proceed with the normal authentication using the matched user's login name
        return wp_authenticate_username_password(null, $username, $password);

    } else {
        // If it's not a phone number, do nothing and let the default login flow continue
        return $user;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.