Skip to content

Instantly share code, notes, and snippets.

@magnific0
Last active June 16, 2023 05:28
Show Gist options
  • Star 36 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save magnific0/29c32c7dabc89ab9cae5 to your computer and use it in GitHub Desktop.
Save magnific0/29c32c7dabc89ab9cae5 to your computer and use it in GitHub Desktop.
Show and Edit User Meta in Wordpress

Show and Edit User Meta in Wordpress

Description

This simple procedure will allow you to:

  1. Display user meta fields under in the user list as additional columns (Users > All Users).
  2. Display these fields on user profiles.
  3. Edit these fields under user edit.

This method works completely without plugins and involves just some functions and hooks in functions.php. Plugins like "User Meta Display" achieve this to some level, but treat custom meta fields completely different from the regular fields. They are shown and edited in seperate environment and fail to show the meta data is a table list. This method integrates custom user meta along with regular user (meta).

Implementation

First it is needed to determine which meta fields are important. The array provides a modular set-up which is flexible and easy for large number of fields. The array key is the meta field name, whereas the array value provides a nice printable name.

function mysite_custom_define() {
  $custom_meta_fields = array();
  $custom_meta_fields['twitter'] = 'Twitter handle';
  $custom_meta_fields['linkedin'] = 'LinkedIn page';
  $custom_meta_fields['facebook'] = 'Facebook profile';
  return $custom_meta_fields;
}

The next two functions create and fill the columns, respectively:

function mysite_columns($defaults) {
  $meta_number = 0;
  $custom_meta_fields = mysite_custom_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 mysite_custom_columns($value, $column_name, $id) {
  $meta_number = 0;
  $custom_meta_fields = mysite_custom_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:

function mysite_show_extra_profile_fields($user) {
  print('<h3>Extra profile information</h3>');

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

  $meta_number = 0;
  $custom_meta_fields = mysite_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>');
}

Next to showing the meta data, saving of changes requires an additional function:

function mysite_save_extra_profile_fields($user_id) {

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

  $meta_number = 0;
  $custom_meta_fields = mysite_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] );
  }
}

Lastly, the functions are integrated in Wordpress through the followig handles.

add_action('show_user_profile', 'mysite_show_extra_profile_fields');
add_action('edit_user_profile', 'mysite_show_extra_profile_fields');
add_action('personal_options_update', 'mysite_save_extra_profile_fields');
add_action('edit_user_profile_update', 'mysite_save_extra_profile_fields');
add_action('manage_users_custom_column', 'mysite_custom_columns', 15, 3);
add_filter('manage_users_columns', 'mysite_columns', 15, 1);    

Sources

The following two posts provided the basis for this solution:

  1. http://wordpress.stackexchange.com/questions/5991/how-to-display-multiple-custom-columns-in-the-wp-admin-users-php
  2. http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields
@FollaKy
Copy link

FollaKy commented Nov 8, 2021

Thanks for this, it really helps!

It works nicely, values are displayed in the columns (and so do they in the database), however they do not show up in the user profile. All fields I added are empty. If I re-save the profile, database get's emptied as well.

Although I could live with this, it would be nice not to have to instruct admins to put all values back in place before saving...

Any suggestions / implementations?

@dannyparrott
Copy link

I made an account in order to thank you for this lovely code. Thanks!!

@magnific0
Copy link
Author

@dannyparrott that's great to hear. I'm happy it is still useful after eight(!) years.

Notice to everyone checking in and asking questions sometimes: I have not used this myself since 2016, so I can't provide support. So help each other out. If a fix gets major attention, I might implement it in the gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment