I am looking to have two separate front end registration forms for adding users to either the partners user group or the members user group. These have been created in the back end with the following user group handles: members and partners.

I have added the following hidden field to the partners registration:

<input type="hidden" name="userGroup" value="partners">

and the following hidden field to the members registration:

<input type="hidden" name="userGroup" value="members">

I have then created a plugin with the following code:

public function init() {


     craft()->on('users.saveUser', function(Event $event) {

      // only fire if new user
      if ($event->params['isNewUser']) {

          // retrieve the userModel from the event
          $user = $event->params['user'];

          // check for presence of 'type' POST field
          $userGroup = craft()->request->getPost('userGroup');

          $userGroups = array();

          if ($userGroup == 'partners') {
              $userGroups[] = craft()->userGroups->getGroupByHandle('partners')->id;

          if ($userGroup == 'members') {
              $userGroups[] = craft()->userGroups->getGroupByHandle('members')->id;

          if (count($userGroups))
              // assign the user to the groups
              craft()->userGroups->assignUserToGroups($user->id, $userGroups);

This code is not adding the user to either of the groups but the user is created with no problem.

  • Just wondering as I am not a plugin expert or even anything like that but did you turn on Public registration in Settings > Users > Settings and also turned off email verification so they aren't put on hold? Commented Feb 16, 2017 at 11:15
  • @JustinDekkers thank you, yeah I did set public registration on and they are being registered just not added to the user group.
    – Gareth
    Commented Feb 16, 2017 at 11:17
  • Found this: github.com/lindseydiloreto/craft-autoassignusergroup Maybe check in the code or maybe it is exactly what you are looking for. Commented Feb 16, 2017 at 12:28
  • @JustinDekkers thank you again. I have looked at that before and it's not doing quite what I need. I just need to figure out why my code is not assigning users to the correct group.
    – Gareth
    Commented Feb 16, 2017 at 12:35
  • @Gareth Have you checked the Craft logs to make sure the post variable is being included with the request? Commented Feb 16, 2017 at 13:54

3 Answers 3


There's a couple of problems you're probably running into.

  1. By default, assigning user permissions (either directly or via groups) requires the the currently logged in user to have the assignUserPermissions permission. It sounds like you're doing this from a front-end request, where I'm guessing there is no authenticated session in place.

  2. User groups and permissions are applied in Craft after the user has already been saved, so even if you got by #1, your changes would probably be overwritten. The events you should be listening for are probably onBeforeAssignUserToGroups and onAssignUserToGroups.

Of course, you can always write a custom plugin that has a controller action, point your form to it, and do whatever custom user saving logic your situation requires as well.

  • Hi Brad, thank's for the help. I am doing this on a front end form and I have just added the code above in my plugin file. I have it working, I hadn't added the hidden input field <input type="hidden" name="userGroup" value="partners"> in the correct form file! Dummy points for me :-) From what your saying though should I not be doing it the way I have?
    – Gareth
    Commented Feb 16, 2017 at 18:23
  • I think what Brad is referring to is that if you set a Default User Group for public registration, then that will get set after the saveUser event has happened. This means it will overwrite the value set using the method in your question. If you leave the Default User Group setting to none it will work as it is. If you want to use the Default User Group setting then you will need to use one of the events that Brad links to. Commented Feb 16, 2017 at 21:17
  • @SteveRowling - taking my ramblings and turning into something useful is a gift you have.
    – Brad Bell
    Commented Feb 16, 2017 at 22:05
  • @SteveRowling many thanks again for all your help in Slack and here. I think what I have works great for what I need then. Thanks too Brad, I always enjoy your ramblings :-)
    – Gareth
    Commented Feb 16, 2017 at 23:09

The code I have in my original question works well for me, you can use it for as many forms/usergroups as you needed just keep adding them as conditionals. I will be sticking with what I wrote. Anyone reading this please look at the comments as I found them very useful in better understanding other ways of solving the problem.


If you're looking for a Craft 3 solution, check out my answer in this ticket: https://craftcms.stackexchange.com/a/27926/4433

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.