Advertisement

Create new user programmatically

For Joomla! 5.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Post Reply
mishaq
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Tue Mar 29, 2011 7:24 am

Create new user programmatically

Post by mishaq » Wed Feb 19, 2025 3:03 pm

Hello,

I am using this code to register a user on fresh Joomla 5.2.4 but not sucessfull and getting error.

Code: Select all

<?php

ini_set("display_errors", 1);
ini_set("display_startup_errors", 1);
error_reporting(E_ALL);


define('_JEXEC', 1);


if (file_exists(__DIR__ . '/defines.php')) {
    require_once __DIR__ . '/defines.php';
}

if (!defined('_JDEFINES')) {
    define('JPATH_BASE', __DIR__);
    require_once JPATH_BASE . '/includes/defines.php';
}


require_once JPATH_BASE . '/includes/framework.php';


use Joomla\CMS\Factory;
use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Application\AdministratorApplication;
use Joomla\CMS\User\UserFactory;
use Joomla\CMS\User\User;
use Joomla\Session\Session;
use Joomla\Session\SessionInterface;


$container = Factory::getContainer();


if (!$container->has(SessionInterface::class)) {
    $container->share(SessionInterface::class, function () {
        return new Session();
    });
}


$session = $container->get(SessionInterface::class);
if (!$session->isStarted()) {
    $session->start();
}


if (!$container->has(CMSApplication::class)) {
    $container->share(CMSApplication::class, function () use ($container) {
        return $container->get(AdministratorApplication::class);
    });
}


$app = $container->get(AdministratorApplication::class);
Factory::$application = $app;


$userFactory = $container->get(UserFactory::class);


$user = new User();
$user->set("name", "John Doe");
$user->set("username", "johndoe123");
$user->set("email", "[email protected]");
$user->set("password", "SecurePass123");
$user->set("groups", [2]); 


if (!$user->save()) {
    echo "User creation failed: " . implode(", ", $user->getErrors());
} else {
    echo "User created successfully! User ID: " . $user->id;
}
Here is error I am getting

Code: Select all

Attempted to load class "Joomla" from namespace "Joomla\Plugin\User\Joomla\Extension".
Did you forget a "use" statement for another namespace?

Advertisement
User avatar
stutteringp0et
Joomla! Ace
Joomla! Ace
Posts: 1482
Joined: Sat Oct 28, 2006 11:16 pm
Location: Texas
Contact:

Re: Create new user programmatically

Post by stutteringp0et » Thu Mar 13, 2025 3:41 pm

It's complaining that it can't find that class - so why not add it to your use statements and give it another go?

use Joomla\Plugin\User\Joomla\Extension\Joomla;

User avatar
ceford
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 3155
Joined: Mon Feb 24, 2014 10:38 pm
Location: Edinburgh, Scotland
Contact:

Re: Create new user programmatically

Post by ceford » Thu Mar 13, 2025 8:58 pm

Why not use the cli: cd to the cli folder and then: php joomla.php user:add --help

User avatar
stutteringp0et
Joomla! Ace
Joomla! Ace
Posts: 1482
Joined: Sat Oct 28, 2006 11:16 pm
Location: Texas
Contact:

Re: Create new user programmatically

Post by stutteringp0et » Thu Mar 13, 2025 11:04 pm

It looks like he wants a one shot script

Advertisement

Post Reply

Return to “Joomla! 5.x Coding”