I have created a WordPress website. In the user section I have created an additional attribute called "Token" meta field. Every time a new user is created or a user logs in and is validated, I would like to update this Token attribute with a random string. I would really appreciate some help in going about doing this. Thank you. This is my code so far during a user log in where randomString is the variable storing the random string.
function login_user($request) {
$length = 10;
$characters =
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
$params = $request->get_params();
$email = $params['email'];
$pass = $params['password'];
$user = get_user_by('email', $email);
$user_id = $user->ID;
if ( $user && wp_check_password( $pass, $user->data->user_pass, $user->ID) ){
return $randomString;
return "true";
}
else {
return "false";
}