Blog

Joomla Add New User With Code

4251 Views

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    = 'jake@testsite.com';
$password = 'password';     

Step 2.
Include Joomla 3 user helper file.

jimport('joomla.user.helper');

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

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

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.

Check If Super Admin

512 Views

While developing my subscription software for Joomla, I found it very difficult to find a solution for determining the super admin. It turns out it was actually a simple solution and this tutorial will show you how to do that.

$user = JFactory::getUser();//first get the logged in user

if($user->get('usertype') =='Super Administrator')://Now check their user type

   // This user is super admin

else:

  // Not super so kick them out!

endif;

That’s it. Now you should be able to get your user admin level. Consider leaving a comment.

Check if user logged in

518 Views

This simple tutorial will show you how to check if a user is logged in.

if( is_user_logged_in() ) {
    // The user is logged in. Do something here
} else{
    // This is a guest. Do something here
}

Add New User to Joomla 2.5.+ and Joomla 3+ from external file

736 Views

Adding new user to Joomla from external file has been one of my popular tutorial but it was not complete since I did not provide the entire codes for the external file. In this tutorial, I will include the entire snippet to add a new user to Joomla. The code includes two sets of includes, one for Joomla 3.0+ and another for lesser version.

Hello world!

415 Views

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!

Welcome to Western Studios

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