You are here

Disable a field in the User "Edit" page. Drupal 7 Hook alter and custom user registration fields.

I need to allow users to add information during registration but not allow them to access those fields to edit after the new user account was saved. (Data collection project, if they went into their profiles and changed that data it would mess up our numbers.)

Here's the Hook alter code. :)

function yourModuleName_form_alter(&$form, &$form_state, $form_id) {
global $user;
    // changed form id
    if ($form_id === 'user_profile_form') {
        // Uncomment the line below to use var_dump  to dump the $form array to see the fields. 
        //var_dump ($form);
 
        if ($user->uid != 1) {
        $form['name_of_field']['#disabled'] = TRUE;
      $form['name_of_a_second_field_if _you_want']['#disabled'] = TRUE;
       
    }
}
}