I am trying to programmatically change a custom field in a user object and I'm having very little success. I want to just use a function to set the field for the object, like field_get_items, except a setter version.

It seems like I could do:

$account = user_load($GLOABALS['user']->uid);

$edit = array(
    'field_custom' => array(
        'und' => array(
            0 => array(
                'value' => $lead_id

user_save($account, $edit)

But there must be a better way to do this.

Any help would be appreciated. Thanks!


1 Answer 1


Use the Entity module wrapper.

The entity API provides some wrapper classes you may use to easily deal with entities and to leverage the entity property information modules provided. With the help of the wrappers you can access the property information, loop over known properties, or just get/set the described data values, etc.

Install the Entity module and do something like:

$user = user_load($GLOBALS['user']->uid);
$obj = entity_metadata_wrapper('user', $user);
$obj->field_custom = $lead_id;

$GLOBALS is http://php.net/manual/en/reserved.variables.globals.php

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.