2012-07-02 62 views
2

Drupal 7我需要使用代碼創建新用戶(不是通過面板手動)。Drupal 7新用戶以編程方式創建?

當我與這個塊的示例代碼我GOOGLE了測試,在Drupal的:

$form_state = array(); 
$form_state['values']['name'] = 'robo-user'; 
$form_state['values']['mail'] = '[email protected]'; 
$form_state['values']['pass']['pass1'] = 'password'; 
$form_state['values']['pass']['pass2'] = 'password'; 
$form_state['values']['op'] = t('Create new account'); 
drupal_execute('user_register', $form_state); 

它顯示:Fatal error: Call to undefined function drupal_execute() in ......

我怎樣才能使它發揮作用或任何其他簡單的方法像上面?

回答

3

您發現的示例代碼是針對Drupal 6並過時的。 drupal_execute()在Drupal 7中更名爲drupal_form_submit(),表單現在爲user_register_form

5

試試這個,它確實以編程方式處理,而不是模仿用戶註冊行爲。

$account->is_new = TRUE; 
$account->status = TRUE; 

$edit['name'] = $name; 
$edit['mail'] = $mail; 
$edit['status'] = 1; 
$edit['init'] = $name; 

$newuser = user_save($account, $edit); 

//send email if you want 
_user_mail_notify('register_no_approval_required', $newuser); 
相關問題