Script to Create New Joomla! 2.5.x / 3.x Users

Off late, I have been getting quite a few calls from some of my clients about their hosting providers warning them about the threat that is Joomla 1.5.x installations. Now, while moving from Joomla 1.5 to Joomla 3.x is well documented but not too easy to follow sometimes, this is not too much of an issue.

But what do you do when someone has attempted and things have gone wrong somewhere…and reverting back to the old Joomla to try again is not an option? Well, that is where carefully managed scripts com into play.

Should you in one way of another, require to create multiple Joomla users, find below a function that will definitely help.

function addJoomlaUser($username, $name, $email, $password) {
jimport('joomla.user.helper');
$salt   = JUserHelper::genRandomPassword(32);
$crypted  = JUserHelper::getCryptedPassword($password, $salt);
$cpassword = $crypted.':'.$salt;

$data = array(
"name"=>$name,
"username"=>$username,
"password"=>$password,
"password2"=>$password,
"email"=>$email,
"block"=>0,
"groups"=>array("1","2")
);


$user = new JUser;
//Write to database
if(!$user->bind($data)) {
throw new Exception("Could not bind data. Error: " . $user->getError());
}
if (!$user->save()) {
//throw new Exception("Could not save user. Error: " . $user->getError());
echo "<br>Could not save user $name - " . $user->getError();
}
return $user->id;
}

Before settling for this script, I had tried many others that try in many ways to mimick the working of the registration function found in Joomla. My experience with such scripts is that they do not always work.

The above script is yet to fail me, especially when looping through a long list of user details – say users who were already registered in a previous system. All that is needed for the script to work it name, username, email and password.

Should you need to generate random passwords like I had to a couple of times to reset user passwords for a site that had been compromised…

$password = JUserHelper::genRandomPassword();

Finally, should you need the script to run independently of Joomla (it is not in an installed extension or something of the sort) find below details on how to do so, should the script be in the root of the Joomla installation where you want to add the users to.

<?php

define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the root. if not in root, change appropriately
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();

$db =& JFactory::getDBO();
$query = "SELECT * FROM user_data";
$db->setQuery($query);
$users = $db->loadObjectlist();

foreach ($users as $user) {
$details = array();
$username = $user->username;
$name = $user->firstname.' '.$user->lastname;
$email = $user->email;
$password = JUserHelper::genRandomPassword();
$return = addJoomlaUser($username, $name, $email, $password);
}

function addJoomlaUser($username, $name, $email, $password) {
jimport('joomla.user.helper');
$salt   = JUserHelper::genRandomPassword(32);
$crypted  = JUserHelper::getCryptedPassword($password, $salt);
$cpassword = $crypted.':'.$salt;

$data = array(
"name"=>$name,
"username"=>$username,
"password"=>$password,
"password2"=>$password,
"email"=>$email,
"block"=>0,
"groups"=>array("1","2")
);

$user = new JUser;
if(!$user->bind($data)) {
throw new Exception("Could not bind data. Error: " . $user->getError());
}
if (!$user->save()) {
throw new Exception("Could not save user. Error: " . $user->getError());
}
return $user->id;
}
?>

Should you have trouble implementing the above scripts…holla in the comments.

Share

10 comments

  1. Hi! can you paste the actual code into a text file or pastebin? I think your blog is formatting the code and adding strange quotes.

    Thanks for putting this out there, it’s exactly what I’m looking for!

  2. Hi Ewiley,
    I have gotten many calls with problems about the template and I have since changed it. please check back and see if you will not be able to easily view the code you want.

  3. The code works, but password not match. The script creates Joomla User, when i try to login in front end, says wrong password.

  4. Thank you very much for this post. Worked out of the box. It really saved me a lot of minutes.

  5. People who not work with the password issue, solution is write codes as below:
    “password”=>$password,
    “password1″=>$password,
    “password2″=>$password,

  6. I copied the aboce code, placed in my root folder named the file login.php
    i’m getting the below error
    0 – Could not save user. Error: JLIB_DATABASE_ERROR_PLEASE_ENTER_YOUR_NAME

    Can you explain what am i doing wrong…?
    using joomla 3.2.4

  7. In my case (Joomla 3.4.3) the user was added to the session, so there was a buggy behaviour when trying to activate the account.

    Just add this line, after $user->save():

    JFactory::getSession()->clear(‘user’, “default”);

    This will remove the newly created user from the session.

Leave a Reply