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 = 
          $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";

You're almost there.

You can use this to update user meta: https://codex.wordpress.org/Function_Reference/update_user_meta.

Create your custom field using ACF (Advanced Custom Fields).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.