Answers.Body: <p>You use <a href="http://buildwithcraft.com/classreference/models/BaseElementModel#setContentFromPost-detail" rel="nofollow">setContentFromPost method</a> on the UserModel if you are posting to your plugin, like so (this is what happens when you save your profile in the control panel):</p>
  
  <pre><code>$user-&gt;setContentFromPost('fields');
  </code></pre>
  
  <p>There is also a <a href="http://buildwithcraft.com/classreference/models/BaseElementModel#setContent-detail" rel="nofollow">setContent method</a>, which takes a <a href="http://buildwithcraft.com/classreference/models/ContentModel" rel="nofollow">ContentModel</a> or an array:</p>
  
  <pre><code>$user-&gt;setContent([ 'fields' =&gt; ['field1' =&gt; 'value'] ])
  </code></pre>
  
  <p>Behind the scenes, that runs the <a href="http://buildwithcraft.com/classreference/models/BaseModel#setAttributes-detail" rel="nofollow">setAttributes method</a>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-26T17:32:18.193Z
Answers.Text: That would have worked if the data were coming from the form post I suppose. But I am entering calculated data.
Answers.UserDisplayName: David A McInnis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-26T18:34:09.803Z
Answers.Text: I added the setContent method. Haven't tested either, but seems like it should work. In general, you should check out the UserModel and the underlying BaseElementModel.
Answers.UserDisplayName: Fred Carlsen
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-26T15:54:39.820Z
Answers.LastActivityDate: 2014-09-26T18:32:54.983Z
Answers.LastEditDate: 2014-09-26T18:32:54.983Z
Answers.UserDisplayName: Fred Carlsen
Answers.Body: <p>Fred got me pointed down a path. Not sure why this works, perhaps someone can explain it to me. This is what I ended up doing.</p>
  
  <pre><code>$initialCredits = 100;
  $user = craft()-&gt;users-&gt;getUserById(craft()-&gt;userSession-&gt;getUser()-&gt;id);
  $user-&gt;getContent()-&gt;initialCredits = $initialCredits;  
  craft()-&gt;users-&gt;saveUser($user);
  </code></pre>
  
  <p>Now, why getContent() worked and setContent() doesn't confuses me. I would think that since I am setting content and not getting it, I should use getContent(). Unless I am on the other side of the model somehow.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-26T20:37:29.150Z
Answers.Text: The User Model is 'getting' content for itself — all about your perspective I guess. ;) Glad you figured it out. This helped me as well. Thanks.
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-26T17:35:34.353Z
Answers.LastActivityDate: 2014-09-26T17:35:34.353Z
Answers.UserDisplayName: David A McInnis
Answers.Body: <p>Fred is definitely on the right path -- the setContent() call works just fine and makes a lot more sense then using getContent():</p>
  
  <pre><code>$initialCredits = 100;
  $user = craft()-&gt;userSession-&gt;getUser();
  $user-&gt;setContent(['initialCredits' =&gt; $initialCredits]);
  craft()-&gt;users-&gt;saveUser($user);
  </code></pre>
  
  <p>P.S.  The way you're getting the current user is redundant.  Just use <code>craft()-&gt;userSession-&gt;getUser()</code> instead.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-14T22:31:07.217Z
Answers.LastActivityDate: 2015-01-14T23:08:09.870Z
Answers.LastEditDate: 2015-01-14T23:08:09.870Z
Answers.UserDisplayName: Paolo
Body: <p>I have added additional fields to my user profiles. I need to be able to change and save these via my plugin. How do I set those values before calling</p>
  
  <pre><code>craft()-&gt;users-&gt;saveUser($user);
  </code></pre>
  
Tags: <plugin-development><users><user-profile>
Title: Save user profile fields through plugin
ThreadText: # Save user profile fields through plugin
  
  (Asked by: username_0 on 2014-09-26)
  
  I have added additional fields to my user profiles. I need to be able to change and save these via my plugin. How do I set those values before calling
  
  ```
  craft()->users->saveUser($user);
  
  ```
  
  **Tags:** plugin-development, users, user-profile
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2014-09-26. Score: 4
  
  You use [setContentFromPost method](http://buildwithcraft.com/classreference/models/BaseElementModel#setContentFromPost-detail) on the UserModel if you are posting to your plugin, like so (this is what happens when you save your profile in the control panel):
  
  ```
  $user->setContentFromPost('fields');
  
  ```
  
  There is also a [setContent method](http://buildwithcraft.com/classreference/models/BaseElementModel#setContent-detail), which takes a [ContentModel](http://buildwithcraft.com/classreference/models/ContentModel) or an array:
  
  ```
  $user->setContent([ 'fields' => ['field1' => 'value'] ])
  
  ```
  
  Behind the scenes, that runs the [setAttributes method](http://buildwithcraft.com/classreference/models/BaseModel#setAttributes-detail).
  
  > **Comments:**
  > **username_0**: That would have worked if the data were coming from the form post I suppose. But I am entering calculated data.  
  > **username_1**: I added the setContent method. Haven't tested either, but seems like it should work. In general, you should check out the UserModel and the underlying BaseElementModel.  
  
  ---
  
  ### Answer by username_0 on 2014-09-26. Score: 2
  
  username_1 got me pointed down a path. Not sure why this works, perhaps someone can explain it to me. This is what I ended up doing.
  
  ```
  $initialCredits = 100;
  $user = craft()->users->getUserById(craft()->userSession->getUser()->id);
  $user->getContent()->initialCredits = $initialCredits;  
  craft()->users->saveUser($user);
  
  ```
  
  Now, why getContent() worked and setContent() doesn't confuses me. I would think that since I am setting content and not getting it, I should use getContent(). Unless I am on the other side of the model somehow.
  
  > **Comments:**
  > **username_2**: The User Model is 'getting' content for itself — all about your perspective I guess. ;) Glad you figured it out. This helped me as well. Thanks.  
  
  ---
  
  ### Answer by username_3 on 2015-01-14. Score: 1
  
  username_1 is definitely on the right path -- the setContent() call works just fine and makes a lot more sense then using getContent():
  
  ```
  $initialCredits = 100;
  $user = craft()->userSession->getUser();
  $user->setContent(['initialCredits' => $initialCredits]);
  craft()->users->saveUser($user);
  
  ```
  
  P.S. The way you're getting the current user is redundant. Just use `craft()->userSession->getUser()` instead.
  
  
  ---
  
  