$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Update an additional user meta field with a string|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Update an additional user meta field with a string

matteradmin9PV0评论

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";
             } 

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";
             } 
Share Improve this question asked Dec 3, 2018 at 9:04 Sanjana NanjappaSanjana Nanjappa 135 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You're almost there.

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

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

Post a comment

comment list (0)

  1. No comments so far