Joe Bloggs
Joe Bloggs

Reputation: 1462

User Meta - Additional Profile Fields - Not showing on Add New User - Wordpress

I am adding a couple of additional User Profile fields in Wordpress user profiles. Everything seems to be working pretty well for view, edit and update, except the fields are not showing up on 'Add New' user profile creation.

I've got the user_register action there add_action('user_register', 'tbc_save_extra_profile_fields', 10, 1);

Also tried with add_action('user_register', 'tbc_show_extra_profile_fields', 10, 1);

But the fields are still not coming up for New accounts.

What am I doing wrong?

Here is my complete code:

function tbc_custom_define() {
  $custom_meta_fields = array();
  $custom_meta_fields['tbc_company_name'] = 'Company name';
  $custom_meta_fields['tbc_company_website'] = 'Company website';
  return $custom_meta_fields;
}

function tbc_show_extra_profile_fields($user) {
  print('<h3>Business information</h3>');

  print('<table class="form-table">');

  $meta_number = 0;
  $custom_meta_fields = tbc_custom_define();
  foreach ($custom_meta_fields as $meta_field_name => $meta_disp_name) {
    $meta_number++;
    print('<tr>');
    print('<th><label for="' . $meta_field_name . '">' . $meta_disp_name . '</label></th>');
    print('<td>');
    print('<input type="text" name="' . $meta_field_name . '" id="' . $meta_field_name . '" value="' . esc_attr( get_the_author_meta($meta_field_name, $user->ID ) ) . '" class="regular-text" /><br />');
    print('<span class="description"></span>');
    print('</td>');
    print('</tr>');
  }
  print('</table>');
}

function tbc_save_extra_profile_fields($user_id) {

  if (!current_user_can('edit_user', $user_id))
    return false;

  $meta_number = 0;
  $custom_meta_fields = cdl_custom_define();
  foreach ($custom_meta_fields as $meta_field_name => $meta_disp_name) {
    $meta_number++;
    update_user_meta( $user_id, $meta_field_name, $_POST[$meta_field_name] );
  }
}
add_action('show_user_profile', 'tbc_show_extra_profile_fields');
add_action('edit_user_profile', 'tbc_show_extra_profile_fields');
add_action('personal_options_update', 'tbc_save_extra_profile_fields');
add_action('edit_user_profile_update', 'tbc_save_extra_profile_fields');
add_action('user_register', 'tbc_save_extra_profile_fields', 10, 1);

Upvotes: 0

Views: 291

Answers (1)

Lucius
Lucius

Reputation: 1333

The hook to new user form is user_new_form:

add_action('user_new_form', 'tbc_show_extra_profile_fields', 99, 1);
add_action('show_user_profile', 'tbc_show_extra_profile_fields', 99, 1);
add_action('edit_user_profile', 'tbc_show_extra_profile_fields', 99, 1);

NOTE:

When user_new_form is called, the first parameter will be:

(string) A contextual string specifying which type of new user form the hook follows.

So, if you are going to use the same tbc_show_extra_profile_fields() to all hooks, remember to handle the $user_id variable properly.

Upvotes: 1

Related Questions