Skip to content

Instantly share code, notes, and snippets.

@broskees
Forked from magnific0/wp_usermeta.md
Created July 19, 2024 21:02
Show Gist options
  • Save broskees/6e653cbe897708e5546dae8ff188be84 to your computer and use it in GitHub Desktop.
Save broskees/6e653cbe897708e5546dae8ff188be84 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment