4757 comments
2071 subscribers
4481 on Twitter
Subscribe! Feed reader E-mail

Drupal: Storing data in the user profile

If you want to store data in the user profile, here’s an example of a quick and dirty way to do it:

global $user;
if ($user) {
  $array = array('fieldname' => 'value_to_save');
  user_save($user, $array);
}

Then access it with $user->fieldname. For more configurability, check out the Profile module.

Short URL: http://sachachua.com/blog/p/4911

5 Responses to “Drupal: Storing data in the user profile”

  1. Hello,

    First of all congratulations on your blog!
    You make me understand you I could load or save data in the user profile! :)

    By the way… maybe you could help me:
    I’ve recently installed Drupal 6.x and Privatemsg module but I can’t see where to send msgs to users.

    Thanks!

    Luís

  2. Follow up – to add data from hook_user, change the $edit array (second parameter).

  3. Hi,
    I need to store data in the data column of the user table.
    How can i do this.

  4. Narsing,

    Use node_save() as in Sacha’s example. It will automatically store the additional user data in data column.

  5. This works in one way but views module doesn’t show the profile values stored in this manner. I guess this is because the data is stored in serialized manner an not in database profile table

Comment, share a thought, ask a question...

Please comment as you, not your organization.





On This Day...

  • 2011: It’s Bike Month in Toronto! — While we don’t have anything like the awesome biking infrastructure of the Netherlands (oh, and all that flat land – [...]
  • 2010: Monthly review: May 2010 — May was quite a learning experience. From an oh-no moment when I accidentally mail-flooded around 70 people to a new [...]
  • 2009: Photos from High Park — High Park
  • 2009: Weekly review: Week ending May 31, 2009 — From last week’s plans: More Transition2 work – finish event-related bugs Another day, another build… Things are progressing nicely! More LinkedIn and [...]
  • 2008: Drupal makes me feel like Bruce Lee + Jackie Chan
  • 2008: Drupal: Adding lines to settings.php in an installation profile — Installation profiles can make it easier for you to test and reproduce your configuration. But what if you need to [...]
  • 2006: Life on the A-list — Somewhere along the way, I managed to end up as the hottest blogger within IBM, with over a thousand hits. I [...]
  • 2006: On the way home after a late night — I’m starving and my hands are a little bit weak. I’ve had nothing but hot chocolate since lunch, too pressed for [...]
  • 2006: RIP, PDA — I discovered to my chagrin this morning that the Compaq iPaq no longer retains a charge, perhaps because its internal battery [...]
  • 2006: Social Tech Brewing — Last night’s Social Tech Brewing social was lots of fun. (Notice how lazy I getabout linking? ;) ) Phillip Smith arrived [...]
  • 2006: It’s alive! Reviving my iPaq — I’ve decided to experiment with carrying an electronic device around again. Several people have reported seeing Moleskines coexist with PDAs, so I [...]
  • 2006: Tagging blog posts — At some point I really should write my own tag indexing thing. =) That way, it’ll be easy to find out, [...]
  • 2006: Blackberry goodness — Sandy read my post on networking and saw my note about Blackberry, that addictive little e-mail-anywhere device. I keep itching [...]
  • 2005: On teaching programming — why do I have to write all this syntactic sugar to just do the canonical “Hello, world”? I firmly believe [...]
  • 2005: Productive day! — I had so much fun writing today. 500 words for my m-ph entry, 1000 for the Linux Journal article on taming [...]
  • 2005: xtla and Gnus — There is a feature in xtla.el to send/review patches via gnus. To set it up, you need the following lines for [...]
  • 2005: Introducing the Hipster PDA — by Sacha Chua (Sneak preview of m-ph entry for tomorrow) “I’ve found the perfect PDA,” I gushed. My friends perked up. Knowing how [...]
  • 2005: Flash fiction: GLUTTONY – 55 words — GLUTTONY (55 words) Flash fiction by Sacha Chua “Gluttony is indecent and a catalyst for sin,” said his devoted mother, measuring rice grains [...]
  • 2004: Imagine Cup — Fanny Sy wrote: I’m so proud to announce that our team representative for the Imagine Cup competition has won the Imagine Cup [...]
  • 2004: Much productive hacking last night — - Dusted off CoursesSubmission and started hacking on it again: minor template tweaks (tables instead of lists, for example) and some [...]
  • 2003: [menu-bar] and mouse bindings — The mouse binding is throwing my memq all off!
  • 2003: proper-listp — (defun proper-listp (l) (or (null l) (and (consp l) (proper-listp (cdr l)))))
  • 2003: Student-centered learning — Brilliant idea! I can make a lot of things learn-by-example, and have them work in pairs almost all the time. For example, [...]
  • 2003: TFI — Dr. Augustin Martin Rodriguez, Gus – program director. Supposed to orient us all on how to be good Ateneo teachers. Useful because [...]
  • 2003: TFI — Introductions roland, math jenny, math andre, DISCS stan, DISCS rico, college 85, pulseasia, strategy and marketing crista, german malou, ecce rex briones, math ramir, math jun, history, institute of philippine [...]