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);
9

It should be something like this:

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

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.