<p>To Create a user do</p>



<pre><code>$request = Craft::$app-&gt;getRequest();
$user = new User();
$user-&gt;username         = $request-&gt;getBodyParam('username');
$user-&gt;email            = $request-&gt;getBodyParam('email');
$user-&gt;newPassword      = $request-&gt;getBodyParam('password');
$user-&gt;firstName        = $request-&gt;getBodyParam('firstName');
$user-&gt;lastName         = $request-&gt;getBodyParam('firstName');
$user-&gt;setScenario(User::SCENARIO_REGISTRATION);
Craft::$app-&gt;getElements()-&gt;saveElement($user);
</code></pre>

<p>To update a user</p>

<pre class="lang-php prettyprint-override"><code>$request            = Craft::$app-&gt;getRequest();
$user-&gt;username     = $request-&gt;getBodyParam('username');
$user-&gt;email        = $request-&gt;getBodyParam('email');
$user-&gt;newPassword  = $request-&gt;getBodyParam('password');
$user-&gt;firstName    = $request-&gt;getBodyParam('firstName');
$user-&gt;lastName     = $request-&gt;getBodyParam('firstName');
$user-&gt;setScenario(User::SCENARIO_LIVE);
Craft::$app-&gt;getElements()-&gt;saveElement($user);
</code></pre>

<p>So it's basically the same except the scenario</p>

CC BY-SA 4.0
2018-11-09T11:48:56.560Z
What if the fields are custom?
Billy Sengsey
CC BY-SA 4.0
2018-11-09T11:55:50.787Z
You mean how to populate fields in the field layout?
Robin Schambach
CC BY-SA 4.0
2019-10-03T13:11:14.933Z
@BillySengsey or @ anyone who arrives here through Google... custom fields are as simple as using the field handle: `$user->customField = 'whatever';` Matrix fields will be a different challenge however...
James Smith
CC BY-SA 4.0
2018-09-25T18:01:52.373Z
2018-09-25T18:01:52.373Z
Robin Schambach
<p>I have 2 forms on my site, one that registers for the WHOLE site and one that registers for just the downloads on the site. I wrote this custom function to change the user group if the email existed. So basically what i'm going for here is if someone signed up for ONLY the downloads and wanted to signup for the whole site to let them use their email they used to sign up originally.</p>

<p>So basically they fill out the sign up form use the same email etc.</p>

<p>Currently the code i have isn't working properly. It changes the user group and allows the user to sign up IF they already signed up, however it won't update the rest of my user fields on the form. If a new user comes to the site and tries to sign up for the whole site the signup form doesn't work, it doesn't save the user, just redirects to the login page.</p>

<p><a href="https://i.sstatic.net/JNiPB.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/JNiPB.png" alt="enter image description here"></a></p>

CC BY-SA 4.0
2018-09-25T17:08:13.993Z
You need to save/create the user in both scenarios if you want to update fields. So to keep it simple (please leave all not necessary information) all your question is about "how to create/update a user"? If I remember correctly you asked this question already a few days ago
Robin Schambach
CC BY-SA 4.0
2018-09-25T17:35:56.773Z
I asked a similar question 13 days ago, i hadn't started developing the functionality yet. How do i go about doing it in both scenarios ?
Kevin Love
<plugin-development><craft3><custom>
Updating craft user fields from plugin
# Updating craft user fields from plugin

(Asked by: username_0 on 2018-09-25)

I have 2 forms on my site, one that registers for the WHOLE site and one that registers for just the downloads on the site. I wrote this custom function to change the user group if the email existed. So basically what i'm going for here is if someone signed up for ONLY the downloads and wanted to signup for the whole site to let them use their email they used to sign up originally.

So basically they fill out the sign up form use the same email etc.

Currently the code i have isn't working properly. It changes the user group and allows the user to sign up IF they already signed up, however it won't update the rest of my user fields on the form. If a new user comes to the site and tries to sign up for the whole site the signup form doesn't work, it doesn't save the user, just redirects to the login page.

[![enter image description here](https://i.sstatic.net/JNiPB.png)](https://i.sstatic.net/JNiPB.png)

**Tags:** plugin-development, craft3, custom
> **Comments:**
> **username_1**: You need to save/create the user in both scenarios if you want to update fields. So to keep it simple (please leave all not necessary information) all your question is about "how to create/update a user"? If I remember correctly you asked this question already a few days ago  
> **username_0**: I asked a similar question 13 days ago, i hadn't started developing the functionality yet. How do i go about doing it in both scenarios ?  

---

## 1 answers

### Answer by username_1 on 2018-09-25. Score: 1

To Create a user do
$request = Craft::$app->getRequest();
$user = new User();
$user->username         = $request->getBodyParam('username');
$user->email            = $request->getBodyParam('email');
$user->newPassword      = $request->getBodyParam('password');
$user->firstName        = $request->getBodyParam('firstName');
$user->lastName         = $request->getBodyParam('firstName');
$user->setScenario(User::SCENARIO_REGISTRATION);
Craft::$app->getElements()->saveElement($user);

To update a user
$request            = Craft::$app->getRequest();
$user->username     = $request->getBodyParam('username');
$user->email        = $request->getBodyParam('email');
$user->newPassword  = $request->getBodyParam('password');
$user->firstName    = $request->getBodyParam('firstName');
$user->lastName     = $request->getBodyParam('firstName');
$user->setScenario(User::SCENARIO_LIVE);
Craft::$app->getElements()->saveElement($user);

So it's basically the same except the scenario

> **Comments:**
> **username_2**: What if the fields are custom?  
> **username_1**: You mean how to populate fields in the field layout?  
> **username_3**: @BillySengsey or @ anyone who arrives here through Google... custom fields are as simple as using the field handle: `$user->customField = 'whatever';` Matrix fields will be a different challenge however...  

---

