How to correctly initialise XenForo environment and create a user?

1.1k views Asked by At

I am trying to create a new XenForo user in PHP.

I am using this example code:

$newusername = "Joseph";
$newpassword = "12345";
$newemail = "[email protected]";

$fileDir = "/home/myfullpath/public_html/members/xenforo";


require( $fileDir . '/library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');

$startTime = microtime(true);
XenForo_Autoloader::initialize($fileDir . '/library', $fileDir);
XenForo_Autoloader::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);

// save user
$result = $writer->save();
echo $result;

The problem is, I get this error:

Fatal error: Call to undefined method XenForo_Autoloader::initialize() in /home/myfullpath/public_html/members/signup/create-user.php on line 13

Line 13 is:

XenForo_Autoloader::initialize($fileDir . '/library', $fileDir);

I checked the Autoloader.php file and couldn't find the initialize method so I don't doubt what PHP is telling me.

How are you meant to load the XenForo environment so you can create a new user? Did they update it in a recent version update or something?

1

There are 1 answers

1
Dale Woods On BEST ANSWER

I found the problem with the code...

I should be targeting the XenForo_Application method not XenForo_Autoloader...

require( $fileDir . '/library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');    

XenForo_Application::initialize($fileDir . '/library', $fileDir);
XenForo_Application::set('page_start_time', $startTime);
XenForo_Application::setDebugMode(true);
XenForo_Application::disablePhpErrorHandler();