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

19 Responses to “Adding extra user meta fields”

  1. Jonnya says:

    Thanks for this – good work, just what I was looking for!

  2. You see, there’s easy, and there’s ‘easy when you know how’. :)

    Seriously, a fantastic piece of work. And fantastic to watch you doing it ‘live’ too. Just as I began to think I was getting pretty good at WordPress…

    • Jan says:

      Hehehe me too. This is actually a powerful tool for those who understand how wordpress works. I am intending to use it with buddypress to achieve so much.. watch me out!

      Thanks for sharing

  3. Gregoire says:

    Hi,

    This is exactly what I was looking for. Thanks you very much. This piece of code saved me a lot of time on my project and gave me a better comprehension of how to write a plugin. Thx !

  4. westi says:

    This should make it even easier in WordPress 2.9 – http://core.trac.wordpress.org/changeset/11784

    Examples of usage on the relevant ticket – http://core.trac.wordpress.org/ticket/10240

  5. Jonnya says:

    Oh it just gets better and better – great news, you guys make it too easy!!

  6. mrdthomas says:

    Great stuff man this help me a lot. Kudos my friend.

  7. cinco says:

    Hi Peter!

    How do I add these fields to user register page in admin panel (user-new.php)?

  8. Richard Tape says:

    Westi, this is brilliant. I’m using this for a plugin I’m creating and I’m also trying to add extra user meta on the ‘Create New User’ page using the user_register action. However, I’m struggling.

    add_action(‘user_register’, array(&$this, ‘action_show_user_profile’));
    add_action(‘user_register’, array(&$this, ‘action_process_options_update’));

    I’ve tried that, but it’s not really doing what I had hoped. Any hints as to what I can do would be greatly appreciated! Thanks again for this.

  9. Richard Tape says:

    Hmm, since my previous post I’ve discovered a couple of things. 1) The user_register appears to be the wrong action to use for my cause. register_form and register_post appear to be the ones I want. 2) After looking at user-new.php in the source code, it seems to explicitly list all of the possible fields for the form without allowing any additions.

    So I’m now not 100% sure that I can do what I want. From another one of your posts it seems that this might be coming in 2.9. Would love to know if you can think of a way to do this already, though?

Leave a Reply

XHTML: You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



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