-
- How to Add a User to a Joomla Group Programmatically
- How to Send Emails in Joomla with PHP
- How to Connect to an External Database in Joomla with PHP
- How to Check if the Current Page Is the Homepage in Joomla with PHP
- How to Check If the Current User is Super User in Joomla with PHP
- How to Create a User Account in Joomla with PHP
- How to Create an Article in Joomla with PHP
- PHP Scripts Collection
How to Add a User to a Joomla Group Programmatically
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;
// Define the user ID to be updated
$userID = 150;
// Load the user object using the Joomla Factory class
$user = Factory::getUser($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->groups))
{
// 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 Apr 23rd 2025 14:04