Drupal 7 开发内部培训资料, 分头诗人

drupal 7

0
投票
1
回答
2340
查看
0
投票
1
回答
3371
查看

Drupal 7 通过代码创建用户?

Drupal 6请移步到 Drupal 6  通过代码创建用户?

很多情况下,我们需要在对某一动作操作时候,需要自动的创建用户,如ubercart中购买产品时候,如没登陆,自动创建用户。这个是如何实现的呢?Drupal 6中的做法,在Drupal 7中会有所改变,看看下面代码。

//This will generate a random password, you could set your own here
  $password = user_password(8);
 
  //set up the user fields
  $fields = array(
    'name' => 'user_name',
    'mail' => 'user_name@example.com',
    'pass' => $password,
    'status' => 1,
    'init' => 'email address',
    'roles' => array(
      DRUPAL_AUTHENTICATED_RID => 'authenticated user',
    ),
  );
 
  //the first parameter is left blank so a new user is created
  $account = user_save('', $fields);
 
  // If you want to send the welcome email, use the following code
 
  // Manually set the password so it appears in the e-mail.
  $account->password = $fields['pass'];
 
  // Send the e-mail through the user module.
  drupal_mail('user', 'register_no_approval_required', $email, NULL, array('account' => $account), variable_get('site_mail', 'noreply@example..com'));

注:你可以附加角色进去,通过role id

页面

订阅 RSS - drupal 7