Adding extra user meta fields

Last night there was a discussion as to what could be achieved today as part of a “WordHack” session at WordCamp UK and SimonD tweeted that he would like to be able to add extra user meta fields to the back end user profile page easily.

I tweeted back that it was already easy and so here is the proof of concept code which shows you:

  • How to add a user meta field to the page
  • How to process the new value when it is updated.

These can the been displayed on the front-end using the standard template tags get_the_author_meta() and the_author_meta()


<?php
/*
Plugin Name: PJW User Meta
Plugin URI: http://blog.ftwr.co.uk/archives/2009/07/19/adding-extra-user-meta-fields
Description: Allows users to configure some random extra meta value.
Author: Peter Westwood
Version: 0.02
Author URI: http://blog.ftwr.co.uk/

Use of the frontend as get_the_author_meta('something') or the_author_meta('something')
*/

class pjw_user_meta {

 function pjw_user_meta() {
 if ( is_admin() )
 {
 add_action('show_user_profile', array(&$this,'action_show_user_profile'));
 add_action('edit_user_profile', array(&$this,'action_show_user_profile'));
 add_action('personal_options_update', array(&$this,'action_process_option_update'));
 add_action('edit_user_profile_update', array(&$this,'action_process_option_update'));
 }

 }

 function action_show_user_profile($user)
 {
 ?>
 <h3><?php _e('Other Contact Info') ?></h3>

 <table>
 <tr>
 <th><label for="something"><?php _e('Something else'); ?></label></th>
 <td><input type="text" name="something" id="something" value="<?php echo esc_attr(get_the_author_meta('something', $user->ID) ); ?>" /></td>
 </tr>
 </table>
 <?php
 }

 function action_process_option_update($user_id)
 {
 update_usermeta($user_id, 'something', ( isset($_POST['something']) ? $_POST['something'] : '' ) );
 }
}
/* Initialise outselves */
add_action('plugins_loaded', create_function('','global $pjw_user_meta_instance; $pjw_user_meta_instance = new pjw_user_meta();'));
?>

Enjoy!

Updated to correctly use the $user/$user_id passed to the actions rather than the global $user_id

22 Responses to “Adding extra user meta fields”

  1. [...] hacking the core, and have these new fields appear in the Dashboard so users can fill them out. In PJW User Meta, Peter Westwood demonstrated how we could add extra fields to the Profile and process them in a [...]

  2. [...] http://prospace.superfreelancer.com. Of course, for pre-2.9 extended user profiles, we should thank Peter Westwood for having originally written this script… I just made some modifications and answered a few [...]

  3. This is really good work – I’m using this and Daniel Quinn’s variation for my directory site. But I have one question – with your plugin, when a registered user (not an admin) visits his/her profile to edit it, do they actually *see* the data they’ve previously entered? Mine is coming up blank every time for the custom fields, even though the fields themselves are visible. Only when logged in as an admin can I see the actual data in those fields… And the data is showing correctly on my author.php page, so I know it’s actually in there…

    • westi says:

      I have just checked and yes I can see them fine when logged in as a non-admin user.

      If you are still having issues drop me an email using my contact form and I’ll see if I can help you solve them.

      • We did get the custom meta data plugin working correctly; now all that’s left is getting the Create Your Community plugin working but that’s a separate matter. When CYC is activated all custom data is hidden; when it’s off, it’s visible.

  4. Kim says:

    For all of those wanting the possibility to create and manage the custom profile fields, give me a signal on my email if you want my custom profile manager plugin :-) it’s not on WP plugins repository because it’s still part of a bigger ecommerce plugin suite. I’ll be glad to strip it out and make it a standalone plugin.

    Greets!

    • Allen says:

      Hey Kim,
      I’d love to get a hold of your custom profile manager plug-in. Thats exactly what I’ve been looking for. Thanks so much for your help, my e-mail is allen(at)wearefunemployed.com.

    • Julie says:

      I would be very interested in trying our your custom profile manager plugin…please. I’ve been digging around for days for a profile manager — seems so logical yet so hard to find. Could I please check it out? Thanks!

  5. Lew A says:

    I actually needed to do something similar, except I wanted to extend the Contact Info to include a Phone, Building, and Room. I also choose to ignore the AIM, Yahoo, and Jabber entries.

    This is what I inserted as a mu-plugin — but it could be added to your functions.php:

    function extended_contact_info($user_contactmethods) {
    //Use this if you want to append to the default contact methods (AIM, Yahoo, Jabber)
    //$user_contactmethods += array(
    // 'building' => __('Building'),
    // 'room' => __('Room'),
    // 'phone' => __('Phone')
    //);

    //Use this if you want to ignore the default contact methods (AIM, Yahoo, Jabber)
    $user_contactmethods = array(
    'building' => __('Building'),
    'room' => __('Room'),
    'phone' => __('Phone')
    );

    return $user_contactmethods;
    }

    add_filter('user_contactmethods', 'extended_contact_info');



d
go to dashboard
l
go to login
h
show/hide help
e
edit post/page
r
comment on post/page
m
go to moderate comments
esc
cancel
%d bloggers like this: