7.x user.api.php hook_user_presave(&$edit, $account, $category)

A user account is about to be created or updated.

This hook is primarily intended for modules that want to store properties in the serialized {users}.data column, which is automatically loaded whenever a user account object is loaded, modules may add to $edit['data'] in order to have their data serialized on save.


$edit: The array of form values submitted by the user. Assign values to this array to save changes in the database.

$account: The user object on which the operation is performed. Values assigned in this object will not be saved in the database.

$category: The active category of user information being edited.

See also



Related topics

6 functions implement hook_user_presave()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

block_user_presave in modules/block/block.module
Implements hook_user_presave().
contact_user_presave in modules/contact/contact.module
Implements hook_user_presave().
entity_crud_hook_test_user_presave in modules/simpletest/tests/entity_crud_hook_test.module
Implements hook_user_presave().
overlay_user_presave in modules/overlay/overlay.module
Implements hook_user_presave().
profile_user_presave in modules/profile/profile.module
Implements hook_user_presave().

... See full list

2 invocations of hook_user_presave()
field_attach_presave in modules/field/field.attach.inc
Perform necessary operations just before fields data get saved.
user_save in modules/user/user.module
Save changes to a user account or add a new user.


modules/user/user.api.php, line 244
Hooks provided by the User module.


function hook_user_presave(&$edit, $account, $category) {

  // Make sure that our form value 'mymodule_foo' is stored as
  // 'mymodule_bar' in the 'data' (serialized) column.
  if (isset($edit['mymodule_foo'])) {
    $edit['data']['mymodule_bar'] = $edit['mymodule_foo'];


donpwinston’s picture

This thing does not work like hook_node_presave. I had to save the user profile form twice for the changes to take effect. Had to resort to using hook_user_profile_submit and user_save() to populate a field so the user would not have to do it. Crazy. I believe in Drupal 8 users will be entities too so maybe it will work the same way then.

If I am wrong and something else must be causing the problem for me then feel free to say so!

TechNikh’s picture

worked for me.

function pp_user_customizations_user_presave(&$edit, $account, $category) {
  $grade_level = $edit['field_grade_level']['und'][0]['value'];
  if($grade_level != "no"){
    $edit['field_title']['und'][0]['value'] = "user_title";
eelkeblok’s picture

Don't know why you are running into the problem that you do, just wanted to point out that users are entities in D7 already.

thedotwriter’s picture

Use the following snippet if you want to know if an user is being created :
<?php if ($account->is_new){ //Do things } ?>

matt2000’s picture

Or use hook_user_insert() instead of this hook.

ElusiveMind’s picture

By the time you get there, the user has already been created. If you want to act on it prior to being created and still determine if it s new, the OP's suggestion is solid.

GrigoriuNicolae’s picture

I did a lightweight role assigning on user registration. Based on http://borutpiletic.net/post/assign-default-role-to-new-registered-user-..., but with slightly updates.

function MYMODULE_user_presave(&$edit, $account, $category) {
  if (isset($account->is_new) && !empty($account->is_new)) {
    $role = user_role_load_by_name('my custom role');
    $edit['roles'] = array($role->rid => 1);
ptmkenny’s picture

himanshupathak3’s picture

I am using this presave function to create a new user password and when I check in devel mode. I see only the plain password is saved.
I see user_hash_password is being called on user_save function. So I think if I set the password in plain text in presave, it should automatically go to user_save function to set the hashed password. But it's not the case. Any ideas ?

danrod’s picture

If you want to cancel user creation from hook_user_presave, you could store a flag in {users}.data indicating that the user is already created, and then call a function from somewhere (maybe from cron) deleting users with that flag.