<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>

CC BY-SA 3.0
2014-09-26T17:32:18.193Z
That would have worked if the data were coming from the form post I suppose. But I am entering calculated data.
David A McInnis
CC BY-SA 3.0
2014-09-26T18:34:09.803Z
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.
Fred Carlsen
CC BY-SA 3.0
2014-09-26T15:54:39.820Z
2014-09-26T18:32:54.983Z
2014-09-26T18:32:54.983Z
Fred Carlsen
<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>

CC BY-SA 3.0
2014-09-26T20:37:29.150Z
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.
Douglas McDonald
CC BY-SA 3.0
2014-09-26T17:35:34.353Z
2014-09-26T17:35:34.353Z
David A McInnis
<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>

CC BY-SA 3.0
2015-01-14T22:31:07.217Z
2015-01-14T23:08:09.870Z
2015-01-14T23:08:09.870Z
Paolo
<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>

<plugin-development><users><user-profile>
Save user profile fields through plugin
# 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 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, which takes a ContentModel or an array:
$user->setContent([ 'fields' => ['field1' => 'value'] ])

Behind the scenes, that runs the setAttributes method.

> **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

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.
$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

Fred 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.


---

