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() {
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'];
// 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.