How can to add multiple users in Joomla 3 without using an extension?
Thank you! :)
How can to add multiple users in Joomla 3 without using an extension?
Thank you! :)
I see in the tags of your question you are using mysql and phpmyadmin, so I assume you are after a a way to INSERT records of users directly into the Joomla database.
However please note that, the exact approach should depend on from where you want to import these users and the actual user-data you already have and want to import.
Also, if these users exist in an another Joomla site of previous versions, then this could be possible either by db migration or by using an extension like SP Tranfer, which would be the simplest and easiest method that would keep ids and other user data with the rest of the site. In such case you shouldn't have to care about other kind of direct db imports.
But let's say you have a simple list of users with names and emails you would like to import, you will need to insert records into 2 db tables for Joomla 3, the #__users
and the #__user_usergroup_map
.
For the #__users
the essential fields are:
name
, username
, email
, password
, block
So, you should end up with an INSERT Query like:
INSERT INTO `#__users` (`name`, `username`, `email`, `password`, `block`)
VALUES
('User1 Name', 'username1', '[email protected]', '$2y$10$Zy4d0JrrUq7Ed6pAzMFtQ.IZf5NtAsMyhDS7AwXlAZqMBgogqCev6', 0),
('User2 Name', 'username2', '[email protected]', '$2y$10$Zy4d0JrrUq7Ed6pAzMFtQ.IZf5NtAsMyhDS7AwXlAZqMBgogqCev6', 0),
('User3 Name', 'username3', '[email protected]', '$2y$10$Zy4d0JrrUq7Ed6pAzMFtQ.IZf5NtAsMyhDS7AwXlAZqMBgogqCev6', 0);
and so on...
Regarding the password field, you could use a default password for all users and then require each user to reset his password in order to login.
For example in the above query, the password field's value $2y$10$Zy4d0JrrUq7Ed6pAzMFtQ.IZf5NtAsMyhDS7AwXlAZqMBgogqCev6
used is for 'test123
', for all the 3 users.
If you have access to the real passwords of each user and want to use these, then you will need to generate them. Find more info about J3 user password encryption here: https://stackoverflow.com/questions/21304038/joomla-3-2-1-password-encryption.
Another note for the #__users
table is the id field (which will be auto-incremented). If you have users with already assigned ids that you want to keep, then you should also include them in your Insert query, otherwise, each user will get automatically his id.
Finally the block value set to 0 will tell Joomla that these users aren't blocked and allowed to use their accounts.
Now, to complete the users import, you will also need to assign all users to a usergroup. This is done, by combining users id with a usergroup. Commonly, the majority of users would go to the Registered usergroup (with default usergroup id =2).
So, you will have to run an INSERT query that will simply do something like:
INSERT INTO `#__user_usergroup_map` (`user_id`, `group_id`)
VALUES
(20, 2),
(21, 2),
(22, 2);
For the user_id
field, you will need to use the ids that have been assigned to your users from the previous Insert.
Hope this will help give you a base to start and adjust accordingly to your specific needs.
Actually the Block field is not required, as it gets a 0 value by default. However I will leave the above answers as is.
I have made an ajax call and then just passing the variables to this script.
define('_JEXEC', 1);
define('JPATH_BASE', __DIR__);
define('DS', DIRECTORY_SEPARATOR);
/* Required Files */
require_once(JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once(JPATH_BASE . DS . 'includes' . DS . 'framework.php');
$app = JFactory::getApplication('site');
$app->initialise();
require_once(JPATH_BASE . DS . 'components' . DS . 'com_users' . DS . 'models' . DS . 'registration.php');
$model = new UsersModelRegistration();
jimport('joomla.mail.helper');
jimport('joomla.user.helper');
$language = JFactory::getLanguage();
$language->load('com_users', JPATH_SITE);
$jinput = JFactory::getApplication()->input;
$type = 0;
$name = $jinput->get('name', '', 'string');
$username = $jinput->get('username', '', 'string');
$email = $jinput->get('email', '', 'email');
$password = $jinput->get('password', '', 'string');
$alias = strtr($name, array(' ' => '-'));
$sendEmail = 1;
$activation = 0;
$data = array(
'username' => $username,
'name' => $name,
'email1' => $email,
'password1' => $password, // First password field
'password2' => $password, // Confirm password field
'sendEmail' => $sendEmail,
'activation' => $activation,
'block' => "0",
'groups' => array("2", "10")
);
$response = $model->register($data);
//print_r($data); //tested
$model->register($data);
Put this script in your root directory. I have tried the script and its working fine.
Let me know if you have any issues.
JInput::getVar
;) It's not a matter of simply replacing JRequest
with JInput
. Have a look here: docs.joomla.org/Retrieving_request_data_using_JInput ;)
$jinput->get('varname', 'default_value', 'filter');
on the code. thanks again @Lodder :)