<p>You'll need to save the new user before you can activate them. This code should do the trick:</p>

<pre><code>if (craft()-&gt;users-&gt;saveUser($newUser)) {
    craft()-&gt;users-&gt;activateUser($newUser);
}
</code></pre>

CC BY-SA 3.0
2016-08-11T15:57:07.270Z
2016-08-11T15:57:07.270Z
Ben Croker
<p>Craft 3 answer:</p>
<pre><code>Craft::$app-&gt;users-&gt;activateUser($savedUser);
</code></pre>

CC BY-SA 4.0
2022-10-12T08:44:22.140Z
2022-10-12T08:44:22.140Z
biko
<p>I'm trying to activate a user via a plugin so that when the user is returned to the login page they are re-directed to their profile page.</p>

<p>I'm making use of the <a href="https://dukt.net/craft/social" rel="nofollow">social logins beta plugin</a> by dukt (I have written directly to them as well) which works fine if the user is already a craft user. However if the user is completely new to craft it throws an error saying the 'Account Has Not Been Activated'. When checking the CMS though the account has been set up and is active. So somewhere along the line the activation seems to be too late and is being delayed until after the user is sent back to the template.</p>

<p>The code creates a new user model in the plugin service with the _registerUser function. Which when looking at the object returned has 'pending =>' so not actually set to anything not even zero.</p>

<p>I've tried a couple of things but neither worked firstly adding </p>

<pre><code>craft()-&gt;users-&gt;activateUser($user);
</code></pre>

<p>directly after saving the new UserModel and before returning the model.</p>

<p>Secondly setting the new UserModel with pending = 0 and pending = '0'</p>

<pre><code>$newUser = new UserModel();
$newUser-&gt;username = $attributes['email'];
$newUser-&gt;email = $attributes['email'];
$newUser-&gt;pending = '0';
</code></pre>

<p>So can anyone shed any light on this or can advise me if this is possible from within the plugin or how the activation works on a new user when you want to automatically activate them without sending an activation email.</p>

<p>Many thanks</p>

<users><usermodel><activation>
How to activate a user in a plugin?
# How to activate a user in a plugin?

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

I'm trying to activate a user via a plugin so that when the user is returned to the login page they are re-directed to their profile page.
I'm making use of the social logins beta plugin by dukt (I have written directly to them as well) which works fine if the user is already a craft user. However if the user is completely new to craft it throws an error saying the 'Account Has Not Been Activated'. When checking the CMS though the account has been set up and is active. So somewhere along the line the activation seems to be too late and is being delayed until after the user is sent back to the template.
The code creates a new user model in the plugin service with the _registerUser function. Which when looking at the object returned has 'pending =>' so not actually set to anything not even zero.
I've tried a couple of things but neither worked firstly adding 
craft()->users->activateUser($user);

directly after saving the new UserModel and before returning the model.
Secondly setting the new UserModel with pending = 0 and pending = '0'
$newUser = new UserModel();
$newUser->username = $attributes['email'];
$newUser->email = $attributes['email'];
$newUser->pending = '0';

So can anyone shed any light on this or can advise me if this is possible from within the plugin or how the activation works on a new user when you want to automatically activate them without sending an activation email.
Many thanks

**Tags:** users, usermodel, activation

---

## 2 answers

### Answer by username_1 on 2016-08-11. Score: 2

You'll need to save the new user before you can activate them. This code should do the trick:
if (craft()->users->saveUser($newUser)) {
    craft()->users->activateUser($newUser);
}


---

### Answer by username_2 on 2022-10-12. Score: 2

Craft 3 answer:
Craft::$app->users->activateUser($savedUser);


---

