Skip to content

Instantly share code, notes, and snippets.

@anishsheela
Last active February 29, 2016 10:11
Show Gist options
  • Save anishsheela/ce320bfba2626018acdf to your computer and use it in GitHub Desktop.
Save anishsheela/ce320bfba2626018acdf to your computer and use it in GitHub Desktop.
Programatically create user in Drupal 7
name = "Programatic user create"
description = "A sample module to create user programaticaly."
core = 7.x
<?php
/**
* @file
* Programatically create user
*/
/**
* Implements hook_menu()
*/
function user_create_menu() {
$items['user_create'] = array(
'access callback' => TRUE,
'page callback' => 'user_create_new',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Page callback to create new user
*/
function user_create_new() {
require_once DRUPAL_ROOT . '/includes/password.inc';
$account = new stdClass;
$account->is_new = TRUE;
$account->name = 'bar';
$account->pass = user_hash_password('foo');
$account->mail = 'foo@example.com';
$account->init = 'foo@example.com';
$account->status = TRUE;
$account->roles = array(DRUPAL_AUTHENTICATED_RID => TRUE);
$account->timezone = variable_get('date_default_timezone', '');
user_save($account);
return array(
'#markup' => 'User Created'
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment