3

how can I update custom user fields programmatically?

I've tried this but don't know how to get Id because I have to execute this code automatically on page load ..

$existingUser = user_load($uid);
$edit = (array) $existingUser; 
$edit['field_company_name']['und'][0]['value'] = 'Ciklum';
user_save($existingUser, $edit);

1 Answer 1

10

It should be something like this:

$existingUser = user_load($uid);
$existingUser -> field_company_name['und'][0]['value'] = "Ciklum";
user_save($existingUser);
9
  • the only issue that I'm facing is that I've many users. How can I update company name for a specific user? Commented Aug 3, 2016 at 6:33
  • Is there any way to load user with other matching condition? Commented Aug 3, 2016 at 6:33
  • @UmairYousaf You can by ex: user_load(5) put the UID number instead.
    – No Sssweat
    Commented Aug 3, 2016 at 6:34
  • 1
    @UmairYousaf you can load user by email like follows user_load_by_mail('[email protected]'); Commented Aug 3, 2016 at 7:14
  • 2
    I would encourage others using entity_metadata_wrapper()
    – Jimmy Ko
    Commented Aug 3, 2016 at 13:18

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.