2

The code below (modified from another post) works fine for me to create a user. But how do you set the new user's frontend language? In the code below, the lines

'frontend-language'=>'de-DE',

'frontend-language'=>$params['frontend-language'],

are how I've unsuccessfully tried to achieve it.

define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the root
define( 'DS', DIRECTORY_SEPARATOR );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

$app = JFactory::getApplication('site');
$app->initialise();

$params = array(
    'name'=>'test1',
    'username'=>'test1_user',
    'email'=>'mailtest1@example.com',
    'password'=>'test',
    'frontend-language'=>'de-DE',
);

$udata = array(
    'name'=>$params['name'],
    'username'=>$params['username'],
    'password'=>$params['password'],
    'email'=>$params['email'],
    'groups'=>array(2), // set the usergroup(s) here (id)
    'frontend-language'=>$params['frontend-language'],
);

$user = new JUser;

try{
    echo '<pre>';
    echo 'Bind user data: ';
    var_dump($user->bind($udata));         

    echo 'Save user data: ';
    var_dump($user->save());

    //echo 'Testing login: ';
    //var_dump( $app->login( array('username'=>$params['username'],'password'=>$params['password']) ) );

} catch (Exception $ex) {

    echo 'Exception: ',  $e->getMessage(), '\n';

}
3

Frontend language field is called language, not frontend-language. It also doesn't have its own column in the database. Instead it's JSON-encoded with other params and stored to params column.

Remove frontend-language element from $udata and instead add params array with language as key and selected language as value:

$udata = array(
    'name' => $params['name'],
    'username' => $params['username'],
    'password' => $params['password'],
    'email' => $params['email'],
    'groups' => array(2), // set the usergroup(s) here (id)
    'params' => array('language' => $params['frontend-language']),
);

Alternatively, you can use JUser::setParam() to set param values:

$user = new JUser;
$user->bind($udata);
$user->setParam('language', 'de-DE');
$user->save();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.