-
- 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 Create a User Account in Joomla with PHP
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.
Below is a snippet on how to create a new Joomla! user account.
use Joomla\CMS\User\User;
use Joomla\CMS\User\UserHelper;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Application\ApplicationHelper;
use Joomla\CMS\String\PunycodeHelper;
/**
* Helper method to create a Joomla! User account
*
* @param String $username The account's username
* @param String $name The account's name
* @param String $email The account's email address
* @param String $password The account's password
* @param Array $groups Comma separated Joomla! User Groups. Defaults to 2 = Registered.
* @param Bool $activate If set to true, the account will be activated without a confirmation e-mail.
*
* @throws Exception
* @return Mixed Object on success
*/
function addJoomlaUser($username, $name, $email, $password, $groups, $activate = false)
{
$data = [
'name' => $name,
'username' => $username,
'password' => $password,
'password2' => $password,
'email' => PunycodeHelper::emailToPunycode($email),
'groups' => explode(',', $groups),
];
if (!$activate)
{
$hash = ApplicationHelper::getHash(UserHelper::genRandomPassword());
$data['activation'] = $hash;
$data['block'] = 1;
}
// Load the users plugin group.
PluginHelper::importPlugin('user');
$user = new User();
if (!$user->bind($data))
{
throw new \Exception($user->getError());
}
if (!$user->save())
{
throw new \Exception($user->getError());
}
return $user;
}
// Create an inactive account. User will need to click on the confirmation e-mail.
$new_user = addJoomlaUser('johndoe', 'John Doe', '[email protected]', '123456', '2');
// Create an active user account. No confirmation e-mail will be sent.
$new_user = addJoomlaUser('johndoe', 'John Doe', '[email protected]', '123456', '2', true);
Last updated on Feb 26th 2026 15:02