Popular Categories
How to add new fields on the Users profile in WordPress?
This simple snippet of codes will allow you to:
- Display additional fields on user profiles.
- Edit these fields under user edit.
- Display user meta fields under in the user list as additional columns (Users > All Users).
The method works completely without plugins and involves just some functions and hooks in functions.php and it integrates custom user meta along with regular user (meta).
Implementation:
First, you need to setup meta fields. We will use a function that sets up the array for additional fields. This way the method is flexible and easy for a large number of fields. The key element to the array entry is the meta field name, whereas the value gives a nice printable name.
$custom_meta_fields = array();
$custom_meta_fields[‘fname’] = ‘First Name’;
$custom_meta_fields[‘lname’] = ‘Last Name’;
$custom_meta_fields[‘company’] = ‘Company’;
$custom_meta_fields[‘answers’] = ‘Answers’;
return $custom_meta_fields;
}
The next two functions create the columns and fill them respectively:
$meta_number = 0;
$custom_meta_fields = custom_fields_define();
foreach ($custom_meta_fields as $meta_field_name => $meta_disp_name) {
$meta_number++;
$defaults[(‘mysite-usercolumn-‘ . $meta_number . ”)] = __($meta_disp_name, ‘user-column’);
}
return $defaults;
}
function users_custom_columns($value, $column_name, $id) {
$meta_number = 0;
$custom_meta_fields = custom_fields_define();
foreach ($custom_meta_fields as $meta_field_name => $meta_disp_name) {
$meta_number++;
if( $column_name == (‘mysite-usercolumn-‘ . $meta_number . ”) ) {
return get_the_author_meta($meta_field_name, $id );
}
}
}
To show this same information on the user profile and edit pages, the following functions are added:
print('<h3>Additional Information</h3>');
print('<table class="form-table">');
$meta_number = 0;
$custom_meta_fields = custom_fields_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 save_extra_profile_fields($user_id) {
if (!current_user_can('edit_user', $user_id))
return false;
$meta_number = 0;
$custom_meta_fields = custom_fields_define();
foreach ($custom_meta_fields as $meta_field_name => $meta_disp_name) {
$meta_number++;
update_usermeta( $user_id, $meta_field_name, $_POST[$meta_field_name] );
}
}
Lastly the functions are integrated in WordPress through the following hooks:
add_action(‘edit_user_profile’, ‘show_extra_profile_fields’);
add_action(‘personal_options_update’, ‘save_extra_profile_fields’);
add_action(‘edit_user_profile_update’, ‘save_extra_profile_fields’);
add_action(‘manage_users_custom_column’, ‘users_custom_columns’, 15, 3);
add_filter(‘manage_users_columns’, ‘custom_columns’, 15, 1);
Was this helpful? Did I miss something? Do you have a question? Get in touch.