我正在使用自定义的Wordpress登录表单。目前,用户可以使用其用户名或电子邮件登录,但我也想向该用户的帐号添加选项。帐号是一个自定义元字段,并且与id不同。
我发现了以下钩子
但是我在线上看到了删除电子邮件用户名的选项,而不是添加元字段的方法。
亲切的问候
答案 0 :(得分: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;
}