Why would you need to load or create a Drupal user from your PHP script ? Because you can associate processes and node importations with a user to identify them quickly into the Drupal content administration ! Then, it’s faster to create it programmatically if it doesn’t exist.
If you want to create or retrieve a Drupal user from your PHP Script, pretty simple:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$account = user_load_by_name('myusername'); if (!is_object($account)) { $form_state = array(); $form_state['values']['name'] = 'myusername'; $form_state['values']['mail'] = 'mail@company.com'; $form_state['values']['status'] = 1; $form_state['values']['pass']['pass1'] = 'mypassword'; $form_state['values']['pass']['pass2'] = 'mypassword'; $form_state['values']['op'] = t('Create new account'); $form_state['values']['roles'] = DRUPAL_AUTHENTICATED_RID; drupal_form_submit('user_register_form', $form_state); $account = user_load_by_name('myusername'); echo "Account Created And Loaded !<br />"; } |
This snippet will load the existing username. If it doesn’t exist, it will create it and load it.
Later you will associate your node with this user:
1 |
$node->uid = $account->uid; |
Recent Comments