Automatically adding users to their chosen security groups during Register [Login]

Hi all,

I’m looking to automatically add users to their chosen security groups as they register using Login / Register Extra.

During registration all users will be added to Group 1:

&usergroups=`Group 1`

… but users will also choose sub-groups to join - either or both of:

  • Group 1-A
  • Group 1-B

After registration [should this be a posthook snippet?] I need to programmatically add the user to either or both sub-groups as they selected in the form.

Any help would be much appreciated.

Chris

using a postHook sounds good

Thanks as ever Bruno.

In the postHook snippet - any ideas how I get the ID of the User who’s just submitted the Register form?

C

According to the documentation, you should be able to get the user with

// A reference to the modUser object
$user = $hook->getValue('register.user');

Thanks Harry - I did find that before but was struggling to work out how to make use of it.

I’ve now got this working as follows:

Snippet "addUsersToSelectedGroups"

$group1A = $hook->getValue('group1A'); // Checkbox in Register form
$group1B = $hook->getValue('group1B'); // Checkbox in Register form
$user = $hook->getValue('register.user');

if ($group1A == 1) {

	$user->joinGroup(5,1); // (Group ID,Role ID)

}

if ($group1B == 1) {

    	$user->joinGroup(4,1); // (Group ID,Role ID)

}

return true;

This is then called in the Register call as:

&postHooks=`addUsersToSelectedGroups`

Any feedback welcome and thanks to @bruno17 and @halftrainedharry

Chris

I could be wrong, but I believe there is no need to call $user->save().

You are correct @halftrainedharry :grin: solution edited

It sounds like you have it sorted, but you might want to take a look at the Subscribe extra, which lets users select both groups and interests themselves and lets them edit them later. The Notify extra works with Subscribe to send emails to selected user groups and/or user-selected interests.

1 Like

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.