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
@tompha
Copy link

tompha commented Jun 5, 2018

Hi there,

The edit function of this doesn't seem to work. Can you please ammend?

Awesome work btw.

Tom

@stevemundey
Copy link

A bit late to the party but I found replacing

update_usermeta( $user_id, $meta_field_name, $_POST[$meta_field_name] );

with:

update_user_meta( $user_id, $meta_field_name, $_POST[$meta_field_name] );

...worked it for me.

@tva3777
Copy link

tva3777 commented Sep 25, 2019

@SteveMunday

A bit late to the party but I found replacing

update_usermeta( $user_id, $meta_field_name, $_POST[$meta_field_name] );

with:

update_user_meta( $user_id, $meta_field_name, $_POST[$meta_field_name] );

...worked it for me.

thanks for the correction

@bradeja
Copy link

bradeja commented Mar 11, 2020

Thank you for this. It work beautifully. How would you go about displaying a different field type, though, such as a WYSIWYG field?

@b4rracuda
Copy link

Thanks man, you really help people out.

@schaefferwarnock
Copy link

Great post, thanks for sharing! However, I am currently using the Gravity Forms User Registration Addon that allows you to create custom user metadata fields from within Gravity Forms. Most of the custom fields that I have created have names like "phone" or "address" so I have used your code like the following:

function mysite_custom_define() {
  $custom_meta_fields = array();
  $custom_meta_fields['phone'] = 'Phone';
  $custom_meta_fields['address'] = 'Address';
  return $custom_meta_fields;
}

but unfortunately this is not displaying the values a user has submitted on the user > profile page. My goal was to be able to edit users metadata after they have submitted it.

Any ideas on how I could accomplish this?

@JosephWesker
Copy link

...
but unfortunately this is not displaying the values a user has submitted on the user > profile page. My goal was to be able to edit users metadata after they have submitted it.

Any ideas on how I could accomplish this?

I am using Gravity Forms 2.4.20.4 and Gravity Forms User Registration Add-On 4.5.8 and works fine.

However at first instance GFUR was using something like 'Phone' instead of 'phone' then because of this I couldn't be able to save or show fields on profile edit page.

Make sure you are using the correct meta value on GFUR feed settings, for this use the select option "Add custom meta" instead of selecting the meta value provided by this dropdown.

@schaefferwarnock
Copy link

schaefferwarnock commented Sep 18, 2020

Thank you @JosephWesker!! Your suggestion was correct and the little mistake I made was simply not matching the case... Such an embarrassing mistake on my part, but I really appreciate your advice and helping me get that working!! My final outcome that worked was as follows:

function mysite_custom_define() {
  $custom_meta_fields = array();
  $custom_meta_fields['Phone'] = 'Phone';
  $custom_meta_fields['Address'] = 'Address';
  $custom_meta_fields['City'] = 'City';
  $custom_meta_fields['State'] = 'State';
  $custom_meta_fields['Zip'] = 'Zip';
  $custom_meta_fields['Division'] = 'Division';
  $custom_meta_fields['Trade'] = 'Trade';
  $custom_meta_fields['Level'] = 'Level';
  return $custom_meta_fields;
}

@laymonk
Copy link

laymonk commented Mar 11, 2021

This should work, but no longer works ... in further testing, I discovered that I am unable to obtain Taxonomy terms from functions.php or plugins like codesnippets. Yes, you can get the term id, but trying to get the term name has been surprisingly problematic,

  • whether using: get_terms() or get_term_by() ... same issue
  • This guy's comment on priorities may be true (but not had a chance to test it out) ... besides, this comment as from 2011, and I ponder if that is still relevant

Mind you, it's actually easier to use UM's own hooks to get at the term_ids:

um_fetch_user( get_current_user_id() );
$mytermid = (int)um_user('taxonomy')[0];
um_reset_user();

Need to get this working ... and will update here if and when I do.

@imkevz
Copy link

imkevz commented May 9, 2021

This is an excellent code! It worked great except that the meta fields utilizing array just display a value of "Array" instead of the actual meta value. Help needed so badly.

@Lewiscowles1986
Copy link

@imkevz, you could check the name of the meta and use implode or pass to a user-defined function. Non scalar values are always going to be a weird edge as whoever is trying to display them will need to know type and hard-code behaviour / workarounds to deal with that.

@diogenesjup
Copy link

Works fine! Tks!
Tks @SteveMunday for the correction in the comments

@magnific0
Copy link
Author

Thanks, I updated the gist with the suggestion by @stevemundey

A bit late to the party but I found replacing

update_usermeta( $user_id, $meta_field_name, $_POST[$meta_field_name] );

with:

update_user_meta( $user_id, $meta_field_name, $_POST[$meta_field_name] );

@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