help in create user meta data when the user is created


I'm trying to create a new user meta data for the created user object when the create user event is triggered

the code is as following:

function organization_user_init()

register_elgg_event_handler('create', 'user', 'org_create_user');

function org_create_user($event, $object_type, $object)
if (($object) && ($object instanceof ElggUser))

$org = "yes";

create_metadata($object->guid, 'org', $org,'text',0, ACCESS_PUBLIC);

elgg_register_event_handler('init', 'system', 'organization_user_init');

Now when i execute this code and trying to get the org varialble using $vars[entity]->org , all the the new and old users are getting this org field as well as they are having the same value.

But what i want to is to create this field and its value only for the every time a new user is created which is the object on which the create event happen.

Please help

  • Check to see if you are actually getting the right guid. Since it's an event handler that's called on save, it might not have guid stored as metadata yet. Try using ->getGUID().

    Other than that, your code looks fine to me.

  • $object should have the correct ->guid in there
    getGUID() will most probably error-out on non objects
    the problem might be that create is perhaps being called for user updates from somewhere..
    (try a dbg dump @ create_metadata(...) code)


  • As Ismayil and D said, I would run that create_metadata after the ->save() for the new user otherwise it won't pick up the guid as the user hasn't been created yet.

  • @T ummhmm:
    guy's gotta do that dbg first to see what values are in there..
    then we proceed from there...
    btw the ->save will be outside the event handler ;)


  • Can any one give me a code example of creating meta data,for the user, when a save or create event happen, and give me another example of how to get all user entities using elgg_get_entities. I tried to get the entites using elgg_get_entities('user','user') in but it give all the more rows than the number of users i have, and it give me also the identical result when i use elgg_get_entities('object','page'). That is will make me crazy. I don't know wether i didn't understand how to use these functions or there is a bug in them. Please help urgently.


  • Have you actually tried these suggestions ?
     * check to see if you are actually getting the right guid.
     * try a dbg dump @ create_metadata(...) code
    you should spend effort in debugging yr own code rather than looking for
    'example code' b/c no one has coded the exact 'example' code u r trying..

    here's sample metadata create code from working plugin :-
    // create_metadata($entity_guid, $name, $value, $value_type, $owner_guid, $access_id = ACCESS_PRIVATE, $allow_multiple = false)
        if ( create_metadata($Guid, "DPay" . $metaname, $metaval,'', 0, ACCESS_PUBLIC) )
            . . .