3

How can to add multiple users in Joomla 3 without using an extension?

Thank you! :)

  • 3
    You can use the User Manager in the Joomla backend – Lodder Feb 20 '16 at 1:33
  • 1
    Add one user at a time - User Manager in Joomla. Add multiple users at a time without using any extension - None – Nagarjun Feb 20 '16 at 4:41
  • do you want to add users by script? – Joomler Feb 20 '16 at 7:33
  • 3
8

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.

Simple DB Import of users

Users Table

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', 'user1@email.com', '$2y$10$Zy4d0JrrUq7Ed6pAzMFtQ.IZf5NtAsMyhDS7AwXlAZqMBgogqCev6', 0),
    ('User2 Name', 'username2', 'user2@email.com', '$2y$10$Zy4d0JrrUq7Ed6pAzMFtQ.IZf5NtAsMyhDS7AwXlAZqMBgogqCev6', 0),
    ('User3 Name', 'username3', 'user3@email.com', '$2y$10$Zy4d0JrrUq7Ed6pAzMFtQ.IZf5NtAsMyhDS7AwXlAZqMBgogqCev6', 0);

and so on...

Passwords

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.

Users ID

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.

Block Field

Finally the block value set to 0 will tell Joomla that these users aren't blocked and allowed to use their accounts.

User_Usergroups Table

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.

UPDATE:

Actually the Block field is not required, as it gets a 0 value by default. However I will leave the above answers as is.

  • 1
    The one and only recommendation I would make to this would be to query the user_id's that are not currently in the user_usergroup_map table but do exist in the users table and then do the insert. A join of some type would probably accomplish that. It just keeps from having to remember what user ids were imported if you are importing a large group of users. Something like this: INSERT INTO #__user_usergroup_map AS m (m.user_id, m.group_id) SELECT u.id FROM #__users AS u WHERE u.id NOT IN (SELECT m.user_id FROM #__user_usergroup_map AS m) – Terry Carter Feb 20 '16 at 18:04
  • That can probably be cleaned up a bit more and shortened but it was off the top of my head without much testing. – Terry Carter Feb 20 '16 at 18:05
  • This also helps if say a user actually registers on your site during your initial insert into the users table. It should grab only users who are no in the map table already. – Terry Carter Feb 20 '16 at 18:06
1

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.

  • 2
    JRequest is deprecated in Joomla 3.x. Please use JInput – Lodder Feb 21 '16 at 1:32
  • 1
    You can't use 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 ;) – Lodder Feb 22 '16 at 9:26
  • oh, I totally forgot to add $jinput->get('varname', 'default_value', 'filter'); on the code. thanks again @Lodder :) – Joomler Feb 22 '16 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.