Posted By

CHEETAH on 04/15/10


Tagged

php object drupal user


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

umang_nine
eval


user object


 / Published in: PHP
 

programmatically create a new user with PHP

  1. <?php
  2. $newUser = array(
  3. 'name' => 'username',
  4. 'pass' => 'password', // note: do not md5 the password
  5. 'mail' => 'email address',
  6. 'status' => 1,
  7. 'init' => 'email address'
  8. );
  9. user_save(null, $newUser);
  10. ?>
  11.  
  12. And, here's how you can update an existing user:
  13.  
  14. <?php
  15. // load user object
  16. $existingUser = user_load('USERID');
  17.  
  18. // update some user property
  19. $existingUser->some_property = 'blah';
  20.  
  21. // save existing user
  22. user_save((object) array('uid' => $existingUser->uid), (array) $existingUser);
  23. ?>
  24.  
  25. If you wanted to update an existing user's profile data:
  26.  
  27. <?php
  28. // load user object
  29. $existingUser = user_load('USERID');
  30.  
  31. // create an array of properties to update
  32. $edit = array(
  33. 'profile_first_name' => 'Eric'
  34. );
  35.  
  36. // save existing user
  37. user_save(
  38. (object) array('uid' => $existingUser->uid),
  39. $edit,
  40. 'Personal Information' // category
  41. );
  42. ?>

Report this snippet  

You need to login to post a comment.