0

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

4
  • 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
    Commented Apr 14, 2018 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
    Commented Apr 17, 2018 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
    Commented Apr 19, 2018 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
    Commented Apr 20, 2018 at 14:33

1 Answer 1

0

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/

1
  • 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
    Commented Apr 21, 2018 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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