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

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.

Step 1.
Create a new .php file in the root of your Joomla installation, this would be the same folder with index.php. Name the file anything, for this example I will use newuser.php

Step 2.
Clear all content in the file then open php tag . Next paste the following codes:

/* [ REMOVE THIS LINE IF YOU ARE USING JOOMLA 2.5 ]
//include joomla framework for Joomla 2.5

define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define('JPATH_BASE', dirname(__FILE__));



require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
JPluginHelper::importPlugin('system');

*/ //[ REMOVE THIS LINE IF YOU ARE USING JOOMLA 2.5 ]

//Error reporting if you need it
//error_reporting(E_ALL | E_NOTICE);
//ini_set('display_errors', 1);


/* [ REMOVE THIS LINE IF YOU ARE USING JOOMLA 3+ ]
//Include framework for Joomla 3+ 
//Set flag that this is a parent file.

const _JEXEC = 1;
		// Load system defines
		if (!defined('JPATH_BASE'))
		{
			define('JPATH_BASE', dirname(__FILE__));
			require_once JPATH_BASE . '/includes/defines.php';
		}
		
		require_once JPATH_BASE . '/includes/framework.php';
		$app = JFactory::getApplication('site');

*/ //[ REMOVE THIS LINE IF YOU ARE USING JOOMLA 3+ ]

//Our class must follow this structure to prevent errors when adding/editing
interface JoomlaUser{
	
	public function addUser($username, $name, $password, $email);
	public function getUser($uid);
	
	
}

//to use this class you must statisfy the above functions. 
class wsJoomla implements JoomlaUser{
	
	public function addUser($username, $name, $password, $email)
	{
		$db = JFactory::getDbo();
		
	   // "generate" a new JUser Object
		$user = new JUser; // it's important to set the "0" otherwise your admin user information will be loaded
	
										
		jimport('joomla.application.component.helper'); // include libraries/application/component/helper.php
		$usersParams = &JComponentHelper::getParams( 'com_users' ); // load the Params
	
		$userdata = array(); // place user data in an array for storing.
	
		//set username
		$userdata['username'] = $username;
	
		//set email
		$userdata['email'] = $email;
	
		//set real name
		$userdata['name'] = $name;
	
		//set password
		$userdata['password'] = $password;
		$userdata['password2'] = $password;//must be set the same as above to confirm password..
	
		//set default group.
		$defaultUserGroup = $usersParams->get('new_usertype', 2);
	
		//default to defaultUserGroup i.e.,Registered
		$userdata['groups']=array($defaultUserGroup);   
		$userdata['block'] = 0; // set this to 0 so the user will be added immediately.
	
	
		 //now to add the new user to the dtabase.
		if (!$user->bind($userdata)) { // bind the data and if it fails raise an error
		
			echo $user->getError();
			return false;
		}
	
	
	
		if (!$user->save()) { // now check if the new user is saved
		   //echo $user->getError();
		 
		  return false;
		}
	
		//if you reach this point then everything went good. You can now use the $user object
		return true;
		
	}
	
	//Get user information here..
	public function getUser($uid)
	{
		
	}
	
	
}


//YOUR CODES HERE...........
$myClass = new wsJoomla;

//grab your posts here
$input = JFactory::getApplication()->input;


$username = $input->post->get('Username');//username/name are the same in this case but you can pass name separately
$email    = $input->post->get('Email', '', 'EMAIL');
$password = $input->post->get('Password');

/*
$username = "testuser";
$email    = "testuser@somesite.com";
$password = "mypassword";
*/

//add user
if($myClass->addUser($username, $username, $password, $email))
{
	echo 'New user added';
	//you can also return json here since you are using the data on another site
	//echo json_encode(array('status'=>true, 'message'=>'New user added'));
}else{
	echo 'Failed to add user';
}

The snippet is made easy to use, if you are using Joomla 2.5 then remove line 1 and line 15. If however you are using Joomla 3 then remove line 22 and line 37

I placed a couple examples on how to use the code. Line 122 – 124 shows you how to get post, this is used when you submit a form to the file or send request from cURl etc.

Line 127 – 129 shows you how to add the user from within the file, this could be if you were looping through an array or need to manually add a user.

Finally, the class is used to add the user to the database and returns true/false so you can process the result.

I hope you find this useful, leave a comment if you do or need some help. Happy coding 🙂

Please Post Your Comments & Reviews

Welcome to Western Studios

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