繁体   English   中英

使用用户名,电子邮件和自定义元字段的Wordpress登录

[英]Wordpress login with username, email and custom meta field

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

我发现了以下钩子

  • wp_authenticate_user
  • wp_authenticate_username_password

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

亲切的问候

您需要在登录表单中添加帐号字段,然后使用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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM