Using Craft::Pro Edition, I want to add a specific UserGroup to an existing User in my module. I tried it this way, but the group is not saved.

$userGroup = Craft::$app->userGroups->getGroupByHandle('partner');
$success = Craft::$app->elements->saveElement($user);

After (successful) saveElement(), $user->getGroups() still returns an empty array.

When I add the group in the Admin CP, it works, but it needs a password confirmation.

1 Answer 1


You can assign a user to certain groups via

Craft::$app->getUsers()->assignUserToGroups($user->id, [1,2,3]);

Would assign them to the groups with the ids 1, 2 and 3

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.