Joomla Add New User With Code

In this tutorial I will teach you how to add a new user to Joomla 3 and above using Joomla core classes.

Step 1.
Most cases you would retrieve the user details from a post such as the username, email and password. This tutorial I will manually add some demo user details..

$realname = 'Jake Test';
$username = 'jake';
$email    = '';
$password = 'password';     

Step 2.
Include Joomla 3 user helper file.


Step 3.
Build an array to store your user data. I used the variable named $udata to store the user details

			  $udata = array(

Step 4.
Other than the obvious, the thing to note here is the groups key in the array. I have it storing some default groups, which is guest and registered, you can also add additional group ids to the array if needed.

The final step is to get a new user object and bind the data array. This will use Joomla core classes to properly store the new user and we can retrieve the user id

     $user = new JUser;
        //Write to database
	if(!$user->bind($udata)) {
		throw new Exception("Could not bind data. Error: " . $user->getError());
	if (!$user->save()) {
		 throw new Exception("Could not save user. Error: " . $user->getError());
     $new_user_id = $user->id;

Now you can easily add a new user to your Joomla 3 and above with php codes. If you find this tutorial helpful and have any questions, please post a comment below.

  1. Gourav Chawla says:

    Hi. Why did you remove the previous code on your website for adding users to joomla? Was there a problem in that code?

    1. mark says:

      There was no problem with that tutorial, I changed the website system and the url is here if this is the tutorial you are referring to?:

  2. Sun Nguyen says:

    I have the 500 error with this line
    if (!$user->save()) {
    throw new Exception(“Could not save user. Error: ” . $user->getError());
    Could u tell me What did I do wrong?

    1. mark says:

      Could you let me know what the error is that you are receiving? Set Joomla debug to maximum.

Please Post Your Comments & Reviews

Welcome to Western Studios

We provide professional website and mobile softwares including Joomla, Wordpress and more.