1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'XenForo Development Discussions' started by Matt777, Aug 25, 2013.

  1. Matt777

    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:
     
  2. Lawrence

    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.
     
  3. Daniel Hood

    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 likes this.
  4. Matt777

    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 !!
     
    Daniel Hood likes this.
  5. NixFifty

    NixFifty Well-Known Member

    I think he meant looking for the code that powers that functionality in the ACP.
     
  6. Matt777

    Matt777 Member

    oh okay, got it, thanks!
     
  7. Matt777

    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();
     
    nhantam, Saiboogu, Marcus and 2 others like this.

Share This Page