Skip to content

Instantly share code, notes, and snippets.

@yehudaTiram
Forked from KostasKrv/wp_usermeta.md
Created August 19, 2021 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yehudaTiram/b2d0c38173ede4b87d73764dc9b4f2f8 to your computer and use it in GitHub Desktop.
Save yehudaTiram/b2d0c38173ede4b87d73764dc9b4f2f8 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 columsn (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 fiedlds 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 one needs to set-up which meta fields are important, it was chosen to go for a modular set-up with an array. This way the method is flexible and easy for large number of fields. The key element to the array entry is the meta field name, whereas the value gives 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 the columns and fill them 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>');
}

Displaying is one thing, however changes need to be saved, hence the last addition.

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_usermeta( $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