<p>Under Settings > Users > Settings Tab there is an option to <code>allow public registration</code> and <code>Default User Group</code> with a drop down that lets you select a user group.</p>

<p>If you need more flexibility, you could also write a simple plugin with an <code>init</code> function defined in the primary plugin class that listens to the <code>saveUser</code> event, and assigns a user group based on some criteria that you define — perhaps test the value of a custom user field on users (that is a hidden field on the registration form).</p>

<p>*UPDATED to assign group based on checkbox field called 'groupPreference':</p>

<pre><code>namespace Craft;
class BusinessLogicPlugin extends BasePlugin {
  public function getName() {
     return 'Business Logic';
  }
  public function getVersion() {
     return '0.1';
  }
  public function getDeveloper() {
     return craft()-&gt;getSiteName();
  }
  public function getDeveloperUrl() {
     return 'http://mysite.com';
  }
  public function init() {
     parent::init();

     craft()-&gt;on('users.saveUser', function(Event $event) {

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

           // retrieve the userModel from the event
           $user = $event-&gt;params['user'];
           // retrieve the groupPreference field content
           $groupPreference = $user-&gt;getContent()-&gt;groupPreference;
           // create an empty array to store group ids
           $groups = array();

           // test for values in groupPreference array
           if (in_array("group1", $groupPreference)) {
              $groups[] = 1;
           }
           if (in_array("group2", $groupPreference)) {
              $groups[] = 2;
           }
           if (in_array("group3", $groupPreference)) {
              $groups[] = 3;
           }
           // assign the user to the groups
           $_POST['groups'] = $groups;
        }         
     });
  }
}
</code></pre>

<p>Haven't tested, but should be pretty close. Please comment if you see an error.</p>

<p>Keep in mind this event will fire, whenever this field is defined, even when saving a user through the CP. So you would have to delete the value before reassigning the user to a different group, or they will just get reassigned.</p>

CC BY-SA 3.0
2017-07-18T23:50:20.237Z
Thanks @LukeClifton. Haven't tested, but I'll take your word for it.
Douglas McDonald
CC BY-SA 3.0
2014-10-05T21:19:40.150Z
I think i might be able to figure something out from this. Have just never dug into plugin development before. Was kinda hoping for a front-end solution like with any other field in an entry form.
paralyzah
CC BY-SA 3.0
2014-10-05T21:52:54.257Z
That's pretty much the whole plugin. Would just need to put it in a folder called businesslogic and drop it in the craft/plugins folder. And then create a field called myHiddenField (or whatever you want) in your user custom fields. May need to tweek the `if ($testCriteria) {...}` statement to get it to work exactly like you want; and add the `php` wrapper. FYI, the name businesslogicplugin is actually a [real plugin created by doublesecretagency](http://straightupcraft.com/craft-plugins/business-logic-template) as a template for creating custom plugins for projects.
Douglas McDonald
CC BY-SA 3.0
2014-10-05T22:00:15.963Z
I imagine that they don't allow the arbitrary assigning of groups from a front-end form because it could present a security risk.
Douglas McDonald
CC BY-SA 3.0
2014-10-05T22:08:17.180Z
I just read your edit: if you make the 'myHiddenField' a dropdown both in the CMS and in the form, you could then test for the value of that field in the `init` function above and assign the group based on the value. I would just be careful not to make the field values 1-to-1 with the groupids, so that someone couldn't make themselves an admin (if they manipulated the post data).
Douglas McDonald
CC BY-SA 3.0
2014-10-05T22:45:17.910Z
I updated the script to test for a custom checkbox field called groupPreference. Not tested.
Douglas McDonald
CC BY-SA 3.0
2014-10-04T21:05:28.403Z
2017-07-18T23:47:47.310Z
2017-07-18T23:47:47.310Z
Douglas McDonald
<p>To have a new user automatically assigned to a user group, there are two easy ways to do it.</p>

<hr>

<p><strong>(1) If the user registers through a front-end form:</strong></p>

<p>You can pre-set their user group by going to Settings > Users > Settings, and selecting the "Allow public registration?" checkbox. This will reveal a dropdown menu, from which you can select their destination user group...</p>

<p><img src="https://i.sstatic.net/6voMF.png" alt="enter image description here"></p>

<p>Any users who sign up after that point will be automatically placed into that user group.</p>

<hr>

<p><strong>(2) If the user is registered through the control panel:</strong></p>

<p>This was an issue I encountered on a few different sites, so I wrote a plugin to easily manage it...</p>

<blockquote>
  <p><a href="https://github.com/lindseydiloreto/craft-autoassignusergroup" rel="nofollow noreferrer">Auto-Assign User Group</a></p>
</blockquote>

<p>It's very easy to use, here's a screenshot:</p>

<p><img src="https://i.sstatic.net/UkY0Y.png" alt="enter image description here"></p>

<p>Any users who are created after that point will be automatically placed into whatever user group(s) you specified.</p>

CC BY-SA 3.0
2014-10-04T22:54:12.980Z
2017-01-12T18:37:46.330Z
2017-01-12T18:37:46.330Z
Lindsey D
<p>How can I assign a user group to a user when it's created through the front end?</p>

<p>EDIT: So a few more details. I see there are some great answers if you want to put all users in one group, but unfortunately that's not the case. I have different forms for different groups. Ideally I would like to have checkboxes on the front end you I can choose which group they are assigned to.</p>

<users><user-groups>
Automatically assign a user group
# Automatically assign a user group

(Asked by: username_0 on 2014-10-04)

How can I assign a user group to a user when it's created through the front end?

EDIT: So a few more details. I see there are some great answers if you want to put all users in one group, but unfortunately that's not the case. I have different forms for different groups. Ideally I would like to have checkboxes on the front end you I can choose which group they are assigned to.

**Tags:** users, user-groups


---

## 2 answers

### Answer by username_1 on 2014-10-04. Score: 7

Under Settings > Users > Settings Tab there is an option to allow public registration and Default User Group with a drop down that lets you select a user group.
If you need more flexibility, you could also write a simple plugin with an init function defined in the primary plugin class that listens to the saveUser event, and assigns a user group based on some criteria that you define — perhaps test the value of a custom user field on users (that is a hidden field on the registration form).
*UPDATED to assign group based on checkbox field called 'groupPreference':
namespace Craft;
class BusinessLogicPlugin extends BasePlugin {
  public function getName() {
     return 'Business Logic';
  }
  public function getVersion() {
     return '0.1';
  }
  public function getDeveloper() {
     return craft()->getSiteName();
  }
  public function getDeveloperUrl() {
     return 'http://mysite.com';
  }
  public function init() {
     parent::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'];
           // retrieve the groupPreference field content
           $groupPreference = $user->getContent()->groupPreference;
           // create an empty array to store group ids
           $groups = array();

           // test for values in groupPreference array
           if (in_array("group1", $groupPreference)) {
              $groups[] = 1;
           }
           if (in_array("group2", $groupPreference)) {
              $groups[] = 2;
           }
           if (in_array("group3", $groupPreference)) {
              $groups[] = 3;
           }
           // assign the user to the groups
           $_POST['groups'] = $groups;
        }         
     });
  }
}

Haven't tested, but should be pretty close. Please comment if you see an error.
Keep in mind this event will fire, whenever this field is defined, even when saving a user through the CP. So you would have to delete the value before reassigning the user to a different group, or they will just get reassigned.

> **Comments:**
> **username_0**: I think i might be able to figure something out from this. Have just never dug into plugin development before. Was kinda hoping for a front-end solution like with any other field in an entry form.  
> **username_1**: That's pretty much the whole plugin. Would just need to put it in a folder called businesslogic and drop it in the craft/plugins folder. And then create a field called myHiddenField (or whatever you want) in your user custom fields. May need to tweek the `if ($testCriteria) {...}` statement to get it to work exactly like you want; and add the `php` wrapper. FYI, the name businesslogicplugin is actually a [real plugin created by doublesecretagency](http://straightupcraft.com/craft-plugins/business-logic-template) as a template for creating custom plugins for projects.  
> **username_1**: I imagine that they don't allow the arbitrary assigning of groups from a front-end form because it could present a security risk.  
> **username_1**: I just read your edit: if you make the 'myHiddenField' a dropdown both in the CMS and in the form, you could then test for the value of that field in the `init` function above and assign the group based on the value. I would just be careful not to make the field values 1-to-1 with the groupids, so that someone couldn't make themselves an admin (if they manipulated the post data).  
> **username_1**: I updated the script to test for a custom checkbox field called groupPreference. Not tested.  
> **username_1**: Thanks @LukeClifton. Haven't tested, but I'll take your word for it.  

---

### Answer by username_2 on 2014-10-04. Score: 3

To have a new user automatically assigned to a user group, there are two easy ways to do it.

---

**(1) If the user registers through a front-end form:**

You can pre-set their user group by going to Settings > Users > Settings, and selecting the "Allow public registration?" checkbox. This will reveal a dropdown menu, from which you can select their destination user group...

![enter image description here](https://i.sstatic.net/6voMF.png)

Any users who sign up after that point will be automatically placed into that user group.

---

**(2) If the user is registered through the control panel:**

This was an issue I encountered on a few different sites, so I wrote a plugin to easily manage it...

> [Auto-Assign User Group](https://github.com/lindseydiloreto/craft-autoassignusergroup)

It's very easy to use, here's a screenshot:

![enter image description here](https://i.sstatic.net/UkY0Y.png)

Any users who are created after that point will be automatically placed into whatever user group(s) you specified.


---

