How can I create a new Joomla user account from within a script?

后端 未结 12 792
感动是毒
感动是毒 2020-12-01 03:41

We\'re creating an XML API for Joomla that allows partner sites to create new accounts for their users on our website.

We\'ve created a standalone PHP script that

12条回答
  •  醉酒成梦
    2020-12-01 04:32

    Based on the answer from waitinforatrain, which is not properly working for logged-in users (actually dangerous if you are using it in the back-end), I have modified it a bit and here it is, fully working for me. Please note that this is for Joomla 2.5.6, while this thread was originally for 1.5, hence the answers above:

    function addJoomlaUser($name, $username, $password, $email) {
        jimport('joomla.user.helper');
    
        $data = array(
            "name"=>$name,
            "username"=>$username,
            "password"=>$password,
            "password2"=>$password,
            "email"=>$email,
            "block"=>0,
            "groups"=>array("1","2")
        );
    
        $user = new JUser;
        //Write to database
        if(!$user->bind($data)) {
            throw new Exception("Could not bind data. Error: " . $user->getError());
        }
        if (!$user->save()) {
            throw new Exception("Could not save user. Error: " . $user->getError());
        }
    
        return $user->id;
     }
    

提交回复
热议问题