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
            $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
        //  craft()->users->activateUser();

1 Answer 1


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.