How do I create a new user directly from PHP code?

Matt777

Member
I'd like to be able to create new registered users directly from php, which would talk to the Xenforo database and just insert new users in.

Is this possible?? :eek:
 

Lawrence

Well-known member
You can add new users manually in the admincp, that would be a good place to start to create an add-on to do this for you.
 

Daniel Hood

Well-known member
It's possible, you have to use the datawriter.

PHP:
$dw = XenForo_DataWriter::create('XenForo_DataWriter_User');
$dw->bulkSet(array('username' => 'username', 'field2' => 'field 2'));
$dw->save();
as an example, you have to look in the datawriter to see the required fields.
 

Matt777

Member
Daniel, thanks! That should get me started at least in the correct direction. Much appreciated. :D (y)

@ Lawrence- thanks for the tip- but that's exactly what I want to avoid, doing it manually in the admincp !!
 

NixFifty

Well-known member
Daniel, thanks! That should get me started at least in the correct direction. Much appreciated. :D (y)

@ Lawrence- thanks for the tip- but that's exactly what I want to avoid, doing it manually in the admincp !!
I think he meant looking for the code that powers that functionality in the ACP.
 

Matt777

Member
Got it working. Here's the code for inserting a new user from any PHP page.

Code:
    $newusername = "Joseph";
    $newpassword = "12345";
    $newemail = "joseph@yahoo.com";
  
    $fileDir = "/home1/myserver/public_html/mywebsite/forums";
  
  
    require($fileDir.'/library/XenForo/Autoloader.php');
    XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');
  
    $startTime = microtime(true);
    XenForo_Application::initialize($fileDir . '/library', $fileDir);
    XenForo_Application::set('page_start_time', $startTime);
    XenForo_Application::disablePhpErrorHandler();
  
    // create new user
    $writer = XenForo_DataWriter::create('XenForo_DataWriter_User');
    // set all the values  
    $writer->set('username', $newusername);
    $writer->set('email', $newemail);
    $writer->setPassword($newpassword, $newpassword);
    $writer->set('user_group_id', XenForo_Model_User::$defaultRegisteredGroupId);
    // $writer->set('location', $location);
    //$writer->set('signature', '[IMG]http://gtracer.net/userbanner/'.$username.'.png[/IMG]');
    // save user
    $writer->save();
 
Top