0

我正在处理一个自定义的 Wordpress 登录表单。目前用户可以使用他们的用户名或电子邮件登录,但我也想添加选项来使用他们的帐号。帐号是自定义元字段,与 id 不同。

我找到了以下钩子

  • wp_authenticate_user
  • wp_authenticate_username_password

但我在网上看到了删除电子邮件用户名的选项,而不是添加元字段的方法。

亲切的问候

4

1 回答 1

0

您需要在登录表单中添加帐号字段,然后使用wp_authenticate_user钩子根据您的需要进行检查或验证。

所以你可以尝试这样的事情:

//Add account number field on login form
add_action( 'login_form', 'myplugin_add_login_fields' );
function myplugin_add_login_fields() {
    $account_number = ( isset( $_POST['account_number'] ) ) ? $_POST['account_number'] : '';
    ?>
    <p>
        <label for="account_number"><?php _e('Account Number','mydomain') ?><br />
            <input type="text" name="account_number" id="account_number" class="input" value="<?php echo esc_attr(stripslashes($account_number)); ?>" size="25" /></label>
    </p>
    <?php
}

//check if account number is present or throw error if its not provided by the user.
//do any extra validation stuff here e.g. get account number from DB
add_filter('wp_authenticate_user','check_account_number', 10, 2);
function check_account_number($user, $password) {
    $return_value = $user;
    $account_number = ( isset( $_POST['account_number'] ) ) ? $_POST['account_number'] : '';
    if(empty($account_number)) {
        $return_value = new WP_Error( 'empty_account_number', 'Please enter account number.' );
    }

    //stop user from logging in if its account number is incorrect
    $account_number_db = get_user_meta($user->ID, 'lidnummer', true);
    if($account_number_db != $account_number) {
        $return_value = new WP_Error( 'invalid_account', 'Please enter your correct account number.' );
    }

    return $return_value;
}
于 2018-11-21T14:45:34.630 回答