You can use this function in functions.php
of your current theme
It will show an extra field on login form
###################Code starts here ###################
<?php
add_action('login_form', 'add_login_field');
function add_login_field()
{
?>
<p>
<label for="user_pass">My Input<br>
<input type="text" name="my_name" value=""/>
</p>
<?php
}
function do_anything($user) {
//do stuff
$userdata= $user = get_userdatabylogin($user);
$user_id = $userdata->ID;
$my_name = $_POST['my_name'];
add_user_meta( $user_id, 'my_name', $my_name);
}
add_action('wp_login', 'do_anything');
?>
############## CODE ENDS HERE ###################
The above code will create an extra field on the login form .
This value will be posted with other values that can be received in “do_anything($user)
” function. Finally this value can be saved as User meta.
Part 2=======================================================================
add_action('login_form', 'add_login_field');
function add_login_field()
{
?>
<p>
<label for="user_pass">My Input<br>
<input type="text" name="my_name" value=""/>
</p>
<?
}
function do_anything($user) {
//do stuff
$userdata= $user = get_userdatabylogin($user);
$user_id = $userdata->ID;
$my_name = $_POST['my_name'];
add_user_meta( $user_id, 'my_name', $my_name);
}
add_action('wp_login', 'do_anything');
add_filter('authenticate', 'check_login', 10, 3);
function check_login($user, $username, $password) {
global $wpdb;
if(isset($_POST['my_name']) && trim($_POST['my_name'])!="")
{
$customfield = $_POST['my_name'];
$UsermetaData = $wpdb->get_results("SELECT *FROM wp_usermeta WHERE meta_value="$customfield"");
$user = get_user_by('id', $UsermetaData[0]->user_id );
// Redirect URL //
if ( !is_wp_error( $user ) )
{
wp_clear_auth_cookie();
wp_set_current_user ( $user->ID );
wp_set_auth_cookie ( $user->ID );
$redirect_to = user_admin_url();
wp_safe_redirect( $redirect_to );
exit();
}
return $user;
}
}
Please check this code snippet. Also you need to modify the code as per requirements. Make sure “my_name” field remain unique and it will not be updated every time on log in.