<p>I used August's answer, but fixed it:</p>

<p>Put this at the top:</p>

<pre><code>use craft\elements\User;
</code></pre>

<p>Then the updating code</p>

<pre><code>$myUser = Craft::$app-&gt;getUser()-&gt;getIdentity();
$myUser-&gt;setFieldValue('fieldHand', 'value');

Craft::$app-&gt;getElements()-&gt;saveElement($myUser, false);
</code></pre>

CC BY-SA 4.0
2018-11-29T10:41:16.527Z
2018-11-29T10:41:16.527Z
Tim Coysh
<p>Be sure and stash a reference to the User element you want to edit:</p>

<pre><code>$myUser = Craft::$app-&gt;getUser()-&gt;getIdentity();
$myUser-&gt;setFieldValue('myUserField', 'New Field Value');

if (Craft::$app-&gt;getElements()-&gt;saveElement($myUser)) {
    echo "User saved.";
}
</code></pre>

<p>Otherwise, you may end up repeatedly fetching the stale record out of the database, instead of saving the modified instance.</p>

<blockquote>
  <p>Note: The similarly-named <code>Craft::$app-&gt;getUsers()</code> is a method for getting the Users Service instance, which is responsible for saving users. Not to be confused with <code>Craft::$app-&gt;getUser()</code>, which is a (Yii) method for fetching the current user (the subsequent call to <code>getIdentity()</code> retrieves the Craft Element for the user).</p>
</blockquote>

CC BY-SA 4.0
2018-11-29T06:47:45.020Z
I get this error now - Calling unknown method: craft\services\Users::saveUser()
David A McInnis
CC BY-SA 4.0
2018-11-29T23:21:48.583Z
@DavidAaronMcInnis Sorry! Updated the answer—took a bad guess at the method, but it turns out you save Users just like any other Element, via the Elements service.
August Miller
CC BY-SA 4.0
2018-11-28T18:56:59.490Z
2019-04-13T23:27:55.907Z
2019-04-13T23:27:55.907Z
August Miller
<p>I have a custom user field 'myuserfield'</p>

<p>How do I save a value to this field via a plugin in Craft3?</p>

<p>Here is my current attempt.</p>

<pre><code>    $myvar = '12345';
    Craft::$app-&gt;getUser()-&gt;getIdentity()-&gt;setFieldValue('myuserfield', $myvar);
    $myid = Craft::$app-&gt;getUser()-&gt;getIdentity()-&gt;getFieldValue('myuserfield');
</code></pre>

<p>When I echo $myid it does, in fact, give me the new value ($myvar or 12345) but when I check the control panel the value is not saved.</p>

<plugin-development><craft3><usermodel>
Saving Custom Field Values to User Profile
# Saving Custom Field Values to User Profile

(Asked by: username_0 on 2018-11-27)

I have a custom user field 'myuserfield'
How do I save a value to this field via a plugin in Craft3?
Here is my current attempt.
    $myvar = '12345';
    Craft::$app->getUser()->getIdentity()->setFieldValue('myuserfield', $myvar);
    $myid = Craft::$app->getUser()->getIdentity()->getFieldValue('myuserfield');

When I echo $myid it does, in fact, give me the new value ($myvar or 12345) but when I check the control panel the value is not saved.

**Tags:** plugin-development, craft3, usermodel

---

## 2 answers

### Answer by username_1 on 2018-11-29. Score: 3 | Accepted answer

I used August's answer, but fixed it:
Put this at the top:
use craft\elements\User;

Then the updating code
$myUser = Craft::$app->getUser()->getIdentity();
$myUser->setFieldValue('fieldHand', 'value');

Craft::$app->getElements()->saveElement($myUser, false);


---

### Answer by username_2 on 2018-11-28. Score: 1

Be sure and stash a reference to the User element you want to edit:
$myUser = Craft::$app->getUser()->getIdentity();
$myUser->setFieldValue('myUserField', 'New Field Value');

if (Craft::$app->getElements()->saveElement($myUser)) {
    echo "User saved.";
}

Otherwise, you may end up repeatedly fetching the stale record out of the database, instead of saving the modified instance.

Note: The similarly-named Craft::$app->getUsers() is a method for getting the Users Service instance, which is responsible for saving users. Not to be confused with Craft::$app->getUser(), which is a (Yii) method for fetching the current user (the subsequent call to getIdentity() retrieves the Craft Element for the user).

> **Comments:**
> **username_0**: I get this error now - Calling unknown method: craft\services\Users::saveUser()  
> **username_2**: @DavidAaronMcInnis Sorry! Updated the answer—took a bad guess at the method, but it turns out you save Users just like any other Element, via the Elements service.  

---

