<p>Yes, it's possible via the special Twig <a href="https://twig.symfony.com/doc/2.x/tags/do.html" rel="nofollow noreferrer"><code>do</code></a> tag, which basically enables you to call methods without returning (i.e. outputting) anything (which is what happens with nearly every other Twig expression).</p>
<p>Assuming the &quot;users field&quot; is a custom field attached to the users' field layout (and you've got a <code>user</code> variable in your Twig template referring to a <a href="https://docs.craftcms.com/api/v3/craft-elements-user.html#public-properties" rel="nofollow noreferrer">User</a> element), something like the below should work:</p>
<pre><code>{% do user.setFieldValue('fieldHandle', 123456) %}
{% do craft.app.elements.saveElement(user) %}
</code></pre>
<p>What the above does, is first setting the custom field value for a field with the handle <code>fieldHandle</code> to a value <code>123456</code> on the <code>user</code> element, via the <a href="https://docs.craftcms.com/api/v3/craft-base-element.html#public-methods" rel="nofollow noreferrer"><code>setFieldValue()</code></a> method (which is available for all Craft element types). Then, the user is (re)saved using the <a href="https://docs.craftcms.com/api/v3/craft-services-elements.html#public-methods" rel="nofollow noreferrer"><code>Elements::saveElement()</code></a> method (which is what Craft uses to save elements under the hood).</p>
<p>In PHP, the same thing would look like this:</p>
<pre class="lang-php prettyprint-override"><code>$user-&gt;setFieldValue('fieldHandle', 123456);
Craft::$app-&gt;getElements()-&gt;saveElement($user);
</code></pre>

CC BY-SA 4.0
2022-03-04T00:50:56.433Z
Thank you so much!
Keatyn Garton
CC BY-SA 4.0
2022-05-23T14:03:58.980Z
Sorry to hijack - is it possible to create a user in this way?
iamkeir
CC BY-SA 4.0
2022-05-23T20:43:16.363Z
@iamkeir Technically yes, I think so. Probably not a good idea, in any case it’s going to be difficult to really answer in a comment so you should probably create a new question for it :)
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2022-05-25T10:06:04.117Z
@MatsMikkelRummelhoff ah the old "just because you can, doesn't mean you should" :) yeah I'll post a new question, thanks.
iamkeir
CC BY-SA 4.0
2022-06-25T11:16:39.337Z
For anyone interested, specific query about creating a user: https://craftcms.stackexchange.com/questions/39591/create-user-in-twig-without-form
iamkeir
CC BY-SA 4.0
2022-03-03T23:42:21.577Z
2022-03-04T00:20:08.130Z
2022-03-04T00:20:08.130Z
Mats Mikkel Rummelhoff
<p>I basically just want to set the value of a users field to a different number if something happens, so that the user field is actually updated in the CMS. Can I do that without creating a form? So like if I had a custom user field I wanted to update in twig and actually have it change the data in the CMS. I know you can do this with a user/save-user form, but I don't really want a form for this because I don't want it to actually be changed by the user.</p>

<templating><users><form><custom-fieldtype><custom-field>
Can I change a user field with twig without using a form?
# Can I change a user field with twig without using a form?

(Asked by: username_0 on 2022-03-03)

I basically just want to set the value of a users field to a different number if something happens, so that the user field is actually updated in the CMS. Can I do that without creating a form? So like if I had a custom user field I wanted to update in twig and actually have it change the data in the CMS. I know you can do this with a user/save-user form, but I don't really want a form for this because I don't want it to actually be changed by the user.

**Tags:** templating, users, form, custom-fieldtype, custom-field


---

## 1 answers

### Answer by username_1 on 2022-03-03. Score: 3 | Accepted answer

Yes, it's possible via the special Twig do tag, which basically enables you to call methods without returning (i.e. outputting) anything (which is what happens with nearly every other Twig expression).
Assuming the "users field" is a custom field attached to the users' field layout (and you've got a user variable in your Twig template referring to a User element), something like the below should work:
{% do user.setFieldValue('fieldHandle', 123456) %}
{% do craft.app.elements.saveElement(user) %}

What the above does, is first setting the custom field value for a field with the handle fieldHandle to a value 123456 on the user element, via the setFieldValue() method (which is available for all Craft element types). Then, the user is (re)saved using the Elements::saveElement() method (which is what Craft uses to save elements under the hood).
In PHP, the same thing would look like this:
$user->setFieldValue('fieldHandle', 123456);
Craft::$app->getElements()->saveElement($user);

> **Comments:**
> **username_0**: Thank you so much!  
> **username_2**: Sorry to hijack - is it possible to create a user in this way?  
> **username_1**: @username_2 Technically yes, I think so. Probably not a good idea, in any case it’s going to be difficult to really answer in a comment so you should probably create a new question for it :)  
> **username_2**: @MatsMikkelRummelhoff ah the old "just because you can, doesn't mean you should" :) yeah I'll post a new question, thanks.  
> **username_2**: For anyone interested, specific query about creating a user: https://craftcms.stackexchange.com/questions/39591/create-user-in-twig-without-form  

---

