How can I set up the user password when I create a new user using the Rules module?

6 Answers 6


You can do it by PHP rule action, first create a function in your custom module as example

function _yourfuction_user_change pass($account,$pass){

Then in your rule in action section select php action and in it's field enter

 _yourfuction_user_change pass($account,'111111'); //your password=111111 and $account = available account parameter in field

and another solution is create your on action


Use the Rules Passwords module.

$pass = 'static_word_' . rand(10,999); //generates a password like "static_word_734" where the 3 numbers are random.
$edit = array(
'pass' => $pass,
user_save($entity_created, $edit); //$entity_created must be replaced with your own variable if you used something else than rules' standard.
//The following part also sets the author of the node to the newly created user.
$uid = $entity_created->uid;
$node->uid = $uid;
$node->is_new = FALSE;
  • I know how to do it programmatically. I needed to use the rules module. Commented Jun 3, 2014 at 15:28

Unfortunately there is no way to create a password with Rules (believe me, I know). What you can do is create the user, and e-mail them the one-time login link with the [entity-created:one-time-login-url] token so that they can login and create their own password.




Make a custom module named "rules_set_password" and place this code inside rules_set_password.rules.inc, it will add a custom action in rules UI named 'Set a user password', is located at 'user' group, it will prompt you with 'account' object and a 'text' variable to be used to set new password value. Do not forget to add your rules_set_password.info file.

 * Implement hook_rules_action_info()
 * Declare any meta data about actions for Rules

 * Custom Rules Actions.
 * https://www.drupal.org/forum/support/post-installation/2011-11-07/creating-user-with-rules-how-to-set-password
function rules_set_password_rules_action_info() {

  $actions['set_user_password'] = array(
    'parameter' => array(
      'account' => array(
        'type' => 'user',
        'label' => t('User'),
      'new_password' => array(
        'type' => 'text',
        'label' => t('New password'),
    'group' => t('user'),
    'label' => t('Set a user password'),
    'base' => 'rules_set_password_rules_action_set_user_password',
  return $actions;

 * Action: Set a user account password.
function rules_set_password_rules_action_set_user_password($account, $new_password) {
  $edit = array('pass' => $new_password);
  user_save($account, $edit);

I can't comment yet, or I would put this in a comment. It is not advisable to set user's passwords. The best practice is to send them a one time login link through their email that directs them to set their own password.

  • 1
    You are right, but question is about "Create user by rules", and what you comment is impossible.
    – pinueve
    Commented Jun 13, 2019 at 19:41
  • I agree, and there are a lot of answers detailing how that works. I was hoping to direct op to a solution that follows best practices. If I were able to comment on op's post directly, I would have. That option is not available to me yet. I need 20 more rep. Commented Jun 13, 2019 at 19:58

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