I want to query the Username field when im in the edit user page and profile page

enter image description here

and fill it into a custom field i have at the end of the form enter image description here

I have my own reasons for the second username field I just want to populate it with the profile Username thanks

  • Hey welcome demo7up, to me its not that clear what exactly your trying to do. Can you explain a bit more? And what have you tried so far? – ngearing Apr 14 '18 at 20:48
  • Hey ok so I have custom fields in wordpress user profiles that have predefined fields that users cant edit. When I as an admin go to edit a users page there is second username field. I want the second username field to query the original username field value and prepopulate the second with the same value as the user im editing. Currently when I hit save the second username field saves with my username instead of the user thats being edited. – demo7up Apr 17 '18 at 23:09
  • How have you added those fields? You should add the query function to your code when you setup those fields up. – ngearing Apr 19 '18 at 2:08
  • All the fields are added and working properly. i just dont know what the query is to query the username from the profile im editing. – demo7up Apr 20 '18 at 14:33

To get the username of a user you can use the get_userdata() function.

Pass it the users ID

$user = get_userdata($user_id);

Then the have access to the username or user_login as its called

echo $user->user_login;

You can read more about this function here: https://codex.wordpress.org/Function_Reference/get_userdata

Or if you need to get the user by other information, see get_user_by() https://developer.wordpress.org/reference/functions/get_user_by/

  • You are the best!!!!!!! Thats exactly what i was looking for I kept using global $current_user; update_usermeta( $user_id, 'fw_name', $current_user->user_login ); This fixed my issue thanks again its been driving me nuts! Everything is working as it should now with your code! – demo7up Apr 21 '18 at 15:51

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.