# MODX 2.8.4: How to get user group in templates?

**URL:** <https://community.modx.com/t/modx-2-8-4-how-to-get-user-group-in-templates/6193>\
**Category:** Development\
**Created:** [December 21, 2022, 3:05pm UTC](https://community.modx.com/t/modx-2-8-4-how-to-get-user-group-in-templates/6193 "2022-12-21T15:05:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![fritzkola](https://community.modx.com/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@fritzkola](https://community.modx.com/u/fritzkola)\
**Post date:** [December 21, 2022, 3:05pm UTC](https://community.modx.com/t/modx-2-8-4-how-to-get-user-group-in-templates/6193/1 "2022-12-21T15:05:15Z")

</div>

Hello,

with [[!+modx.user.id]] modx delivers the user id of current user.  
And with :userinfo=`email` for example I can get email adress without using a custom snippet.

Is there a trick to get the usergroups?

Thank you in advance.

bye  
Chris

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [December 21, 2022, 3:12pm UTC](https://community.modx.com/t/modx-2-8-4-how-to-get-user-group-in-templates/6193/2 "2022-12-21T15:12:16Z")

</div>

> [@fritzkola](#):
>
> Is there a trick to get the usergroups?

I don’t think this is possible with an output modifier.  
You probably have to write a custom snippet.

* * *

There is an output modifier to check if a user is in a certain group,

```html
[[!+modx.user.id:memberof=`Administrator`]]

```

but that is probably not what you want.

---

<div class="post-metadata">

**Author:** ![fritzkola](https://community.modx.com/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@fritzkola](https://community.modx.com/u/fritzkola)\
**Post date:** [December 21, 2022, 3:22pm UTC](https://community.modx.com/t/modx-2-8-4-how-to-get-user-group-in-templates/6193/3 "2022-12-21T15:22:04Z")

</div>

Sounds good. But what is the output of memberof?

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [December 21, 2022, 3:28pm UTC](https://community.modx.com/t/modx-2-8-4-how-to-get-user-group-in-templates/6193/4 "2022-12-21T15:28:07Z")

</div>

I believe it’s either `1` (the user is in the group) or `0`.

---

<div class="post-metadata">

**Author:** ![fritzkola](https://community.modx.com/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@fritzkola](https://community.modx.com/u/fritzkola)\
**Post date:** [December 21, 2022, 3:37pm UTC](https://community.modx.com/t/modx-2-8-4-how-to-get-user-group-in-templates/6193/5 "2022-12-21T15:37:01Z")

</div>

> [@halftrainedharry](#):
>
> `[[!+modx.user.id:memberof=`Administrator`]]`

ok, this is what i expected too. But I only get the userid in all cases. 🤔

Oh, I tried:

`[[!+modx.user.id:memberof=`Administrator`:then=`Yes`:else=`No`]]`

this runs…

many thanks 🙏

---

<div class="post-metadata">

**Author:** ![fritzkola](https://community.modx.com/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@fritzkola](https://community.modx.com/u/fritzkola)\
**Post date:** [December 21, 2022, 4:30pm UTC](https://community.modx.com/t/modx-2-8-4-how-to-get-user-group-in-templates/6193/6 "2022-12-21T16:30:15Z")

</div>

additional question:

If I programmatically change the user group, how could I refresh permissions for this user, so that it s active?

---

<div class="post-metadata">

**Author:** ![bobray](https://community.modx.com/user_avatar/community.modx.com/bobray/32/46_2.png) [@bobray](https://community.modx.com/u/bobray)\
**Post date:** [December 22, 2022, 7:36am UTC](https://community.modx.com/t/modx-2-8-4-how-to-get-user-group-in-templates/6193/7 "2022-12-22T07:36:11Z")

</div>

I think if you use `$user->joinGroup({groupName or id});` that will happen automatically. If not, something like this should do it:

`$modx->user->loadAttributes(array(), 'web', true);`

BTW, it sounds like you’ve solved your problem, but if you need to list or retrieve all the user’s user groups, this will do it for the current user (this is for others who find this page when searching):

```auto
$output = "";
$memberGroups = $modx->user->getMany('UserGroupMembers', 
      array('member' => $modx->user->get('id')));

foreach($memberGroups as $memberGroup) {
    $group = $memberGroup->getOne('UserGroup');
    $output .= "<br>" . $group->get('name');
}
return $output;

```

---

<div class="post-metadata">

**Author:** ![fritzkola](https://community.modx.com/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@fritzkola](https://community.modx.com/u/fritzkola)\
**Post date:** [December 22, 2022, 11:54am UTC](https://community.modx.com/t/modx-2-8-4-how-to-get-user-group-in-templates/6193/8 "2022-12-22T11:54:47Z")

</div>

oh, that is fine.

thank you very much. 🙏

---

<div class="post-metadata">

**Author:** ![joshualuckers](https://community.modx.com/user_avatar/community.modx.com/joshualuckers/32/4244_2.png) [@joshualuckers](https://community.modx.com/u/joshualuckers)\
**Post date:** [December 23, 2022, 9:38am UTC](https://community.modx.com/t/modx-2-8-4-how-to-get-user-group-in-templates/6193/9 "2022-12-23T09:38:18Z")

</div>

The following two methods on `modUser` are useful as well:

- `getUserGroups()`
- `getUserGroupNames()`

---

<div class="post-metadata">

**Author:** ![bobray](https://community.modx.com/user_avatar/community.modx.com/bobray/32/46_2.png) [@bobray](https://community.modx.com/u/bobray)\
**Post date:** [December 24, 2022, 8:52pm UTC](https://community.modx.com/t/modx-2-8-4-how-to-get-user-group-in-templates/6193/10 "2022-12-24T20:52:06Z")

</div>

@joshualuckers Doh!. Thanks for being so polite about it. For most use cases, those are much better approaches than the one I suggested. 😉
