How to Add a User to a Joomla Group Programmatically

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.

Learn how to add a user to a specific Joomla group using PHP. This script is handy for automating user management tasks or integrating Joomla with other systems. It uses Joomla's Factory class to load a user, update their group assignments, and save the changes.


use Joomla\CMS\Factory;
use Joomla\CMS\User\UserFactoryInterface;

// Define the user ID to be updated
$userID = 150;

// Load the user object
$userFactory = Factory::getContainer()->get(UserFactoryInterface::class);
$user = $userFactory->loadUserById($userID);

// Define the new group ID to add the user to
$newGroupId = 8;

// Check if the user is not already in the specified group
if (!in_array($newGroupId, $user->getAuthorisedGroups()))
{
    // Add the new group ID to the user's groups
    $user->groups[] = $newGroupId;
}

// Attempt to save the updated user object
if (!$user->save())
{
    // Output an error message if saving fails
    echo "Failed to add user to the group: " . $user->getError();
}
else
{
    // Output a success message if saving succeeds
    echo "User successfully added to the group.";
}

To add a user to the Super Users group, you must have Super Users privileges.
Last updated on Mar 5th 2026 14:03