[Solved] Create user setting via hook "snippet"

Hello, I’m registering an user using the [[!Register? snippet.
Everything is working fiene but.

I need that based on one of the form fields that is displaying a list of resources :

<select id="resource" name="resource">
	 <option value=""></option>
		&tpl=`@INLINE: <option value="[[+id]]">[[+pagetitle]]</option>`

and using &postHooks=setSetting

I need to assign the setting tree_root_id with the value from the resource field (code above) to that new registered user.

Can not find much information about creating an user setting, can anyone help me please?
Thank you very much!

Maybe this thread helps:

OK @halftrainedharry mate, problem solved, thanks again and again, as always!!!

Using the [[!Register? snippet.

There is an snippet called setSetting in a hook:

This code will run after the user is registered, it will get the ID of the new registered user using the username field and set the setting tree_root_id
The value of the setting comes from another input dropdown with a list of resources with each resource ID’s as value.

snippet setSetting:

$id = "";
$username = $hook->getValue('username');
$resource = $hook->getValue('resource');

// get registered user ID via username field
$user = $modx->getObject('modUser', array(
    'username' => $username,
if ($user) {
    $profile = $user->getOne('Profile');
	$id = $profile->get('id');

// set setting for the user
$setting = $modx->getObject('modUserSetting', [
    'key' => 'tree_root_id',
    'user' => $id
if (!$setting) {
    $setting = $modx->newObject('modUserSetting');
    $setting->set('key', 'tree_root_id');
    $setting->set('value', $resource);
    $setting->set('user', $id);

return true;

That is it, hope it’s useful for somebody else.

Thanks! :+1:

Just make sure to handle the case when for some reason the user doesn’t exists, in the case the first ‘if’ isn’t meet, you’ll be setting an null on the second query, and will end up with a silent error, as nothing will actually happens, but no exception will be thrown

@camicase82 Because it’s inside a Hook this code will run only in case the user is registered successfully, otherwise will no run. :+1:

Use the id from the user and not from the profile.

But more important, according to the login documentation, you can get a reference to the user object directly from the hook.

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

There is no need to query via the username field. Just use

$user = $hook->getValue('register.user');
$id = $user->get('id'); //user id