---
title: "Assign New Users to a Different User Group Automatically in Joomla - Code Snippets"
description: "Put every new member straight into the user group you choose, instead of moving accounts by hand after each sign-up, using one PHP snippet."
url: "https://www.tassos.gr/docs/code-snippets/howto/access-control-security/auto-assign-user-group"
date: "2026-10-23T00:39:53+00:00"
language: "en-GB"
---

[ Home ](https://www.tassos.gr/docs) / [ Code Snippets ](https://www.tassos.gr/docs/code-snippets) / [ How-to Guides ](https://www.tassos.gr/docs/code-snippets/howto) / [ Access Control & Security ](https://www.tassos.gr/docs/code-snippets/howto/access-control-security)

#  Assign New Users to a Different User Group Automatically in Joomla

##  Put every new member straight into the user group you choose, instead of moving accounts by hand after each sign-up, using one PHP snippet.

 ![Assign New Users to a Different User Group Automatically in Joomla](https://www.tassos.gr/images/2026/10/code-snippets-auto-assign-user-group-card.png) Heads up! This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.

Joomla puts every new account into one group, the one set under **Users** → **Options** → **New User Registration Group**. When your members should start as Authors, or in a custom Members group with its own access level, you end up moving each account by hand. You can assign new users to a different user group automatically in Joomla with one PHP snippet in Code Snippets.

## [The PHP snippet code](#the-php-snippet-code)

The snippet runs right after Joomla stores a new account and moves it into the group you choose. Profile edits and existing accounts aren't touched.

```
use Joomla\CMS\Factory;
use Joomla\CMS\Helper\UserGroupsHelper;
use Joomla\CMS\User\UserHelper;

// The ID of the group new users join. Find it under Users -> Groups.
$groupId = 3;

// true: the group above replaces the default group from the Users options.
// false: new users join the group above on top of the default one.
$replaceDefault = true;

// Do not edit below.

$app = Factory::getApplication();

$app->registerEvent('onUserAfterSave', function ($event) use ($groupId, $replaceDefault)
{
    $isNew  = $event->getArgument('isNew', $event->getArgument(1, false));
    $saved  = $event->getArgument('savingResult', $event->getArgument(2, false));
    $user   = $event->getArgument('subject', $event->getArgument(0, []));
    $userId = (int) ($user['id'] ?? 0);

    // Only new accounts that Joomla saved. Profile edits pass through.
    if (!$isNew || !$saved || !$userId)
    {
        return;
    }

    // A group that doesn't exist would break the registration, so check first.
    if (!UserGroupsHelper::getInstance()->get((int) $groupId))
    {
        return;
    }

    if ($replaceDefault)
    {
        UserHelper::setUserGroups($userId, [(int) $groupId]);

        return;
    }

    UserHelper::addUserToGroup($userId, (int) $groupId);
});
```

Change the 2 settings at the top:

- `$groupId`: the ID of the group new users join. Go to **Users** → **Groups** and read it from the **ID** column. On a fresh Joomla site, Registered is 2, Author is 3 and Editor is 4.
- `$replaceDefault`: keep `true` to make this group the only one a new user belongs to. Set it to `false` to add the group on top of the default one.

When the ID doesn't match any group, the snippet does nothing and the new user keeps the default group, so a typo can't break registration.

 Never point $groupId at Administrator or Super Users. Everyone who fills in the registration form would get full access to your site.

## [How to add this snippet](#how-to-add-this-snippet)

1. Install [Code Snippets](https://www.tassos.gr/docs/code-snippets/getting-started/how-to-install) if it isn't installed already.
2. Log into your Joomla backend.
3. Go to **Components** → **Tassos Code Snippets**.
4. Click **New**.
5. Enter a title, for example *New users to Authors*.
6. Set **Code Type** to **PHP**.
7. Set **Insertion Method** to **Auto-Insert Locations**.
8. Set **Location** to **On Page Load**.
9. Paste the code above into the editor.
10. Replace the sample group ID with your own.
11. Click **Save & Close**.

## [Use a different group for one registration page](#use-a-different-group-for-one-registration-page)

When your site has more than one registration page, for example one for customers and one for partners, limit the snippet to the page whose sign-ups should land in the new group. Open the snippet, go to the **Conditional Logic** tab, set **Display on** to **Selected Pages**, add a **Menu** or **URL - Query String** condition that matches that page, and save. Sign-ups from the other pages keep the default group.

Learn more in [Conditional Logic](https://www.tassos.gr/docs/code-snippets/functionality/conditional-logic).

## [Check that it works](#check-that-it-works)

1. Open your site's registration page in a private browser window and create a test account.
2. In your Joomla backend, go to **Users** → **Manage** and open the new account.

The **Assigned User Groups** tab shows the group from `$groupId` ticked, and with `$replaceDefault` set to `true` the default group isn't.

To send members of each group to their own page after they log in, see [Redirect Users After Login by User Group in Joomla](https://www.tassos.gr/docs/code-snippets/howto/access-control-security/redirect-after-login). To learn more about how PHP snippets work, read the [PHP snippet docs](https://www.tassos.gr/docs/code-snippets/types/php).

 Last updated on Oct 15th 2026 18:10
