<p>Declare a private variable inside your class, <code>private $userId = NULL</code></p>

<p>After you've saved the user in the first method, update the variable <code>$this-&gt;userId = $user-&gt;id</code></p>

<p>In the second method grab the user by using the variable <code>$user = craft()-&gt;users-&gt;getUserById($this-&gt;userId);</code></p>

<p>I haven't actually tested the code, but it might do the trick :)</p>

<p>EDIT: You might want to make a null-check before attempting to fetch the user, to ensure that it doesn't break if it tries to run it while the variable is still <code>NULL</code></p>

CC BY-SA 3.0
2016-05-04T10:17:52.263Z
2016-05-04T10:17:52.263Z
Fuglsetrampen
<p>This is a super basic question...</p>

<p>I've got a plugin that watches two events, in one event it creates a user. After another event I want it to activate that user.</p>

<p>How do I get/store/pass the id of the newly created user from the first event to the second?</p>

<pre><code>public function init()
{
    craft()-&gt;on('thing.beforeEvent', function(Event $event){
        // Save the new user if they're not already a user
        if(!craft()-&gt;userSession-&gt;getUser()){
            $newUser = new UserModel;

            $newUser-&gt;firstName = craft()-&gt;request-&gt;getPost('firstName');
            $newUser-&gt;lastName  = craft()-&gt;request-&gt;getPost('lastName');
            $newUser-&gt;username  = craft()-&gt;request-&gt;getPost('username');
            $newUser-&gt;email     = craft()-&gt;request-&gt;getPost('email');
            $newUser-&gt;password  = craft()-&gt;request-&gt;getPost('password');

            $newUserSuccess = craft()-&gt;users-&gt;saveUser($newUser);
            if (!$newUserSuccess)
            {
                Craft::log('Couldn’t save the new user', LogLevel::Error);
            }
        }
    });

    craft()-&gt;on('thing.afterEvent', function(Event $event){
        // Activate the new user if they're not already a user
        //if(!craft()-&gt;userSession-&gt;getUser()){
        //  craft()-&gt;users-&gt;activateUser();
        //}
    });
}
</code></pre>

<plugin-development>
Storing variables in a plugin
# Storing variables in a plugin

(Asked by: username_0 on 2016-05-04)

This is a super basic question...
I've got a plugin that watches two events, in one event it creates a user. After another event I want it to activate that user.
How do I get/store/pass the id of the newly created user from the first event to the second?
public function init()
{
    craft()->on('thing.beforeEvent', function(Event $event){
        // Save the new user if they're not already a user
        if(!craft()->userSession->getUser()){
            $newUser = new UserModel;

            $newUser->firstName = craft()->request->getPost('firstName');
            $newUser->lastName  = craft()->request->getPost('lastName');
            $newUser->username  = craft()->request->getPost('username');
            $newUser->email     = craft()->request->getPost('email');
            $newUser->password  = craft()->request->getPost('password');

            $newUserSuccess = craft()->users->saveUser($newUser);
            if (!$newUserSuccess)
            {
                Craft::log('Couldn’t save the new user', LogLevel::Error);
            }
        }
    });

    craft()->on('thing.afterEvent', function(Event $event){
        // Activate the new user if they're not already a user
        //if(!craft()->userSession->getUser()){
        //  craft()->users->activateUser();
        //}
    });
}

**Tags:** plugin-development

---

## 1 answers

### Answer by username_1 on 2016-05-04. Score: 3 | Accepted answer

Declare a private variable inside your class, `private $userId = NULL`

After you've saved the user in the first method, update the variable `$this->userId = $user->id`

In the second method grab the user by using the variable `$user = craft()->users->getUserById($this->userId);`

I haven't actually tested the code, but it might do the trick :)

EDIT: You might want to make a null-check before attempting to fetch the user, to ensure that it doesn't break if it tries to run it while the variable is still `NULL`


---

