Adding Custom User Meta to WordPress User Profiles

Monday, August 24th 2009

UPDATE: Hi. If you're running WordPress 2.9, reconsider this approach, as there is now a way to add custom user meta via core functions. Visit Justin Tadlock's blog for a short and sweet summary of how to do this.

So far, you've got a nice, browsable archive of your registered users thanks to Members List, with each user's profile being more or less infinitely customizable through the Author template. Plus users can upload their own photos. Naturally, the next step is to extend user profiles with our own custom user meta.

On (the very hacky) Rokkyuu.com, I used an ancient plugin called Register Plus to create new user meta, and had to hack the core to display the new Profile fields in the Dashboard. Hacking the WordPress core sucks, and Register Plus is more or less an abandoned plugin, with very unstable innards. Since we've come this far, we want something more reliable.

As I mentioned before, we're still waiting on WordPress 2.9 (or later) to provide us with the necessary filters to alter the core profile template in the Dashboard. In the meantime, we can create a plugin that will add new fields to the Profile. The plugin you create would be specific to your theme/project, but since it's a plugin, it wouldn't involve hacking the core. We start with a proof of concept from Peter Westwood with a few of my changes:

<?php
/*
Plugin Name: Custom User Meta
Plugin URI: http://blog.ftwr.co.uk/archives/2009/07/19/adding-extra-user-meta-fields
Description: Allows users to configure extra user meta values.
Author: Peter Westwood, Daniel Quinn
Version: 0.02
Author URI: http://www.dquinn.net

Add new custom user meta fields to the user's Profile in the WordPress Dashboard.

*/

class custom_user_meta {

function custom_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 class="form-table">
    <tr>
        <th><label for="extra1"><?php _e('Extra Value'); ?></label></th>
        <td><input type="text" name="extra1" id="extra1" value="<?php echo esc_attr(get_the_author_meta('extra1', $user->ID) ); ?>" /></td>
    </tr>
    </table>
    <?php
}

function action_process_option_update($user_id) {
    update_usermeta($user_id, 'extra1', ( isset($_POST['extra1']) ? $_POST['extra1'] : '' ) );
}

}

/* Initialise outselves */
add_action('plugins_loaded', create_function('','global $custom_user_meta_instance; $custom_user_meta_instance = new custom_user_meta();'));
?>

Save this as "my-custom-user-meta.php" in the plugins folder under /custom-usermeta/. Activate the plugin.

You should now see an extra area in the Profile called "Other Contact Info" with a single new custom user meta field: Extra Value. All users now have access to this new value. The best part is that you can call this value in your Author template as you would any other user meta. To add it to your definition list:

<dt>Extra Custom Value</dt>
<dd><?php echo $curauth->something; ?></dd>
<dt>Twitter</dt>
<dd><?php echo $curauth->twitter; ?></dd>
<dt>Textarea</dt>
<dd><?php echo $curauth->bio2; ?></dd>

You can add any fields you want, first by simply adding new table rows (or entire tables)...

<table class="form-table">
    <tr>
        <th><label for="extra1"><?php _e('Extra Value'); ?></label></th>
        <td><input type="text" name="extra1" id="extra1" value="<?php echo esc_attr(get_the_author_meta('extra1', $user_id) ); ?>" /></td>
    </tr>
    <tr>
        <th><label for="twitter"><?php _e('Twitter Username'); ?></label></th>
        <td><input type="text" name="twitter" id="twitter" value="<?php echo esc_attr(get_the_author_meta('twitter', $user_id) ); ?>" /></td>
    </tr>
    <tr>
        <th><label for="bio2"><?php _e('Bio2'); ?></label></th>
        <td><textarea name="bio2" cols="10" rows="10" id="bio2"><?php echo esc_attr(get_the_author_meta('bio2', $user_id) ); ?></textarea></td>
    </tr>
</table>

...and second, by processing them:

update_usermeta($user_id, 'extra1', ( isset($_POST['extra1']) ? $_POST['extra1'] : '' ) );
update_usermeta($user_id, 'twitter', ( isset($_POST['twitter']) ? $_POST['twitter'] : '' ) );
update_usermeta($user_id, 'bio2', ( isset($_POST['bio2']) ? $_POST['bio2'] : '' ) );

If you want to display each value only if the user has filled it out, you could do something like:

<?php if ($curauth->something) { ?>
    <dt>Extra Custom Value</dt>
    <dd><?php echo $curauth->something; ?></dd>
<?php } ?>
<?php if ($curauth->twitter) { ?>
    <dt>Twitter</dt>
    <dd><?php echo $curauth->twitter; ?></dd>
<?php } ?>
<?php if ($curauth->bio2) { ?>
    <dt>Textarea</dt>
    <dd><?php echo $curauth->bio2; ?></dd>
<?php } ?>

Simple stuff. And thanks for the snippet, Peter.

UPDATE: Some of you asked how you would handle radio buttons, dropdowns, and checkboxes. It's no different than you would normally do these types of input boxes in any PHP form. Remember that with checkboxes, the idea is that the user is choosing one or more items in your list (and the $_POST values from the checkboxes are added to a checkbox array), and with radio buttons, the user is choosing one of the available options. The latter is true of dropdowns too:

<tr>
     <th><label for="penguins"><?php _e('Favorite Penguin'); ?></label></th>
     <td>
          <select name="penguins" id="penguins">
               <option value="" <?php if (esc_attr(get_the_author_meta('penguins',$user->ID)) == "") { ?>selected<?php } ?>>- Choose a Penguin -</option>
               <option value="Happy Penguin" <?php if (esc_attr(get_the_author_meta('penguins',$user->ID)) == "Happy Penguin") { ?>selected<?php } ?>>Happy Penguin</option>
               <option value="Sad Penguin" <?php if (esc_attr(get_the_author_meta('penguins',$user->ID)) == "Sad Penguin") { ?>selected<?php } ?>>Sad Penguin</option>
               <option value="Ugly Penguin" <?php if (esc_attr(get_the_author_meta('penguins',$user->ID)) == "Ugly Penguin") { ?>selected<?php } ?>>Ugly Penguin</option>
          </select>
     </td>
</tr>
<tr>
    <th><?php _e('Potatoes I Like'); ?></th>
    <td>
        <?php
          $potato_array = get_the_author_meta('potatoes',$user->ID);
        ?>
        <ul>
          <li><input value="Hot Potato" id="hot_potato" name="potatoes[]" <?php if (is_array($potato_array)) { if (in_array("Hot Potato",$potato_array)) { ?>checked="checked"<?php } }?> type="checkbox" /> Hot Potato</li>
          <li><input value="Cold Potato" id="cold_potato" name="potatoes[]" <?php if (is_array($potato_array)) { if (in_array("Cold Potato",$potato_array)) { ?>checked="checked"<?php } } ?> type="checkbox" /> Cold Potato</li>
          <li><input value="Salty Potato" id="salty_potato" name="potatoes[]" <?php if (is_array($potato_array)) { if (in_array("Salty Potato",$potato_array)) { ?>checked="checked"<?php } } ?> type="checkbox" /> Salty Potato</li>
        </ul>
    </td>
</tr>
<tr>
    <th><?php _e('Monkeys'); ?></th>
    <td>
         <?php
           $monkeys = get_the_author_meta('monkeys',$user->ID);
         ?>
        <ul>
            <li><input value="Blue Monkey" id="blue_monkey" name="monkeys" <?php if ($monkeys == "Blue Monkey") { ?>checked="checked"<?php } ?> type="radio" /> Blue Monkey</li>
            <li><input value="Red Monkey" id="red_monkey" name="monkeys" <?php if ($monkeys == "Red Monkey") { ?>checked="checked"<?php } ?> type="radio" /> Red Monkey</li>
            <li><input value="Yellow Monkey" id="yellow_monkey" name="monkeys" <?php if ($monkeys == "Yellow Monkey") { ?>checked="checked"<?php } ?> type="radio" /> Yellow Monkey</li>
        </ul>
    </td>
</tr>

In the case of the dropdowns or the radio button, we are checking to see if the variable already exists, and if it does, we add "checked" for radio buttons to the appropriate button, or "selected" for options in a dropdown. In the case of the checkboxes, we need to add the "checked" attribute to any checkboxes that exist in the checkbox array. I've added these values to my dev area, so you can try it for yourself.

UPDATE: Apparently the variables you use need to be all lowercase. Rose (a commenter below) had a problem with fields in the profile not saving if the variables were camelCase. Can anyone else confirm? WP does recommend all-lowercase variables in its coding standards with underscores for spaces, but I didn't realize camelCase would simply not be saved in the db by WP? Server config? Dunno.

UPDATE: Check out Vlad's sweet implementation of this plugin: 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 questions ;)

UPDATE: A commenter named Ruben below has shared how he figured out sorting the custom meta fields outside the profile. Check it out in Dutch: http://rubenwoudsma.nl/wordpress-gebruikersprofiel-uitbreiden/

UPDATE: Peter made an update to the plugin, replacing the call to global $user_id with $user->ID. This is reflected in the updated code.

Your Comments

Much Ado About Nothing

166 comments

rss feed

  1. Ryan Yockey Monday, August 31, 2009 at 1:37 am

    Excellent guide here. A nick quick way to pop some custom values into the system.

    Thanks for the guide.

  2. Dave Munden Tuesday, September 1, 2009 at 1:24 pm

    Hi - have been looking at CIMY User Extra plugin. But struggling to echo the new fields on an author template. Which is better? Cimy or this plugin? Thanks for great tips!

    • Daniel Quinn Tuesday, September 1, 2009 at 1:32 pm

      I don't believe you can access Cimy's custom fields unless you use his function, which is specific to that plugin (I may be wrong so you might want to double check). I tend to stay away from plugins that store the custom field data in a different place than (or provide separate functions for) where WordPress puts/retrieves the data because then it means you end up relying entirely on the plugin. I prefer Flutter because you can access the custom field data it adds through the normal WP functions in your templates. So if want the backend GUI niceties for custom field data, use Flutter and then use WP's built-in functions to call the custom fields.

      • Dave Munden Tuesday, September 1, 2009 at 2:59 pm

        Very quick reply - thanks. I think I agree with 'purer' ways of extending wordpress. Which BTW is why I am a huge fan of Justin Tadlock. But one other q: with CIMY you at least get additional fields under User Profile in Admin. How can I get these new fields into a User Profile area? Flutter creates new write panels for posts and pages only - right? Many thanks for help in advance.

        • Daniel Quinn Tuesday, September 1, 2009 at 3:38 pm

          Right, Flutter is only for posts/pages. To add custom user meta to the Profile (for both users to access in the Dashboard and you as an admin to be able to echo into templates), you'd use the plugin from this article. Again, I'm not sure if in Cimy the usermeta is or isn't stored someplace unusual (database-wise), but with the above plugin, you have full control over the placement of the fields, messages near them, etc.

          Of course, until we get the filter() methods that are being cooked up in WP 2.9, we can't modify the placement of the default fields in the Profile w/o hacking the core.

          • Barry Thursday, September 3, 2009 at 4:50 am

            Great tutorial, thanks a lot!

            Im getting syntax errors with that plugin code though. Any ideas?

            Thanks again!

            • Daniel Quinn Thursday, September 3, 2009 at 6:38 am

              Hi Barry - what errors are you getting?

              • Barry Thursday, September 3, 2009 at 8:59 am

                Parse error: syntax error, unexpected '}' in /var/www/web4/web/wp-content/plugins/custom-usermeta/my-custom-user-meta.php on line 36

                • Daniel Quinn Thursday, September 3, 2009 at 9:09 am

                  Ah, you'll need to add opening/closing PHP brackets to the block of code (added these in the example as well).

                  • Barry Thursday, September 3, 2009 at 9:18 am

                    Thanks mate, im new to this lark, learning loads from your tutorials!

                    • Barry Thursday, September 3, 2009 at 10:53 am

                      One more quick question, when im adding extra fields using:

                      type="textarea" rows="10" cols="60"

                      the input box stays the same size?

                      any idea what im doing wrong?

  3. Daniel Quinn Thursday, September 3, 2009 at 11:32 am

    @Barry - WP has its own stylesheet for the Dashboard. You need to override the stylesheet with something like style="width: 600px"; or attach your own stylesheet via a plugin/filter.

    • Barry Thursday, September 3, 2009 at 11:45 am

      Worked a charm, thank you!

      • Daniel Quinn Thursday, September 3, 2009 at 1:07 pm

        Cool beans.

        • Barry Tuesday, September 15, 2009 at 7:37 am

          This, i promise, is the VERY LAST time i will seek assistance from you; But i am -it turns out- RUBBISH at PHP. I have used an array to make a drop down menu in the user profile section:

          But the selection of the dropdown menu is not saved when clicking 'update profile'.

          Any help at all would be amazing

          • Daniel Quinn Wednesday, September 16, 2009 at 10:11 am

            Could you email me the code you were trying to post? Why not hardcode the dropdown values?

          • Jillian Madison Sunday, September 20, 2009 at 9:47 pm

            Barry, I'm having the exact same problem. The information that's been input into the textarea is not being saved when I click "update profile."

            Did you ever figure this out? I've been at it for 2 days and it's driving me crazy.

            • Daniel Quinn Sunday, September 20, 2009 at 10:15 pm

              Hi Jillian, I just tested this in the dev site and it's working - make sure that the esc_attr() part is in between the opening and closing textarea tags (textarea does not have a value attribute - the value is in between the opening and closing textarea tags). I'll add an example to the tutorial above :)

              • Jillian Madison Sunday, September 20, 2009 at 11:39 pm

                Thanks for taking the time to help me, Daniel! Your code worked perfectly. I don't know who you are, but you are the frickin' MAN.

                I'll be bookmarking you for sure.

                Jill @ FNH

  4. Dan F Sunday, September 6, 2009 at 2:08 pm

    I get errors with the "auto approval" fix for User Photo. Specifically:

    Fatal error: Call to undefined function: add_action() in /nfs/c01/h04/mnt/44829/domains/investigativecompliance.com/html/beta/wp-includes/functions.php on line 3353

    Any idea?

    • Daniel Quinn Sunday, September 6, 2009 at 2:18 pm

      Are you adding the code to your functions file in your theme, or the core functions file?

  5. realistdreamer Monday, September 7, 2009 at 4:08 am

    Thanks so much for this. I've bookmarked you as there is great info here. I was searching around to compare Cimy, DRegister and Register Plus. Cimy seems the only one living and it doesn't use user meta (I checked).

    Given my alternatives, I'm considering using it because of my fear of building some of functionality I need that appears built in to Cimy's new release (8/16/09).

    •Added dropdown-multi support
    •Added file upload support
    •Added rules check also during profile update (WP >= 2.8.x only)
    •Added possibility to see up to 5000 users per page on A&U Extended page
    •Has avatar and picture upload

    Waiting for better integration of MU & BuddyPress. Until then, looking to use Cimy, Alkivia Open Community and bbPress in a niche site that needs more "community."

    Question:
    I know it would be a pain, but I'm assuming I could move data to user meta after a suitable solution arrives???

    • Daniel Quinn Monday, September 7, 2009 at 11:41 am

      It's a good question - presumably with enough knowledge of how WP stores the usermeta it wouldn't be impossible, but what if the only way to move the data is by doing it manually? Could you imagine moving it for like three or four hundred users? Though now that you mention there's a new version of Cimy out, I'll check it out. Also Alkivia looks interesting. Maybe it's a lot easier to move the data than we think?? I will get back to you.

      Thanks for your thoughts!

      • realistdreamer Tuesday, September 8, 2009 at 1:56 am

        I'm continuing my research and beginning to think I'm just going to bite the bullet and get WPMU and BuddyPress. I only need one blog, but Buddypress features for my users which I've been trying to cobble together with plugins. Given the impending merger of the WP/WPMU codebases, I thinking just get started with the more robust versions.

        Can you think of a reason I shoold stick with basic WP? Any great plugins that won't work? Can I still use Tadlock's Hybrid theme Framework?

        • Daniel Quinn Tuesday, September 8, 2009 at 7:19 am

          What are your requirements? WPMU and BuddyPress might be more effective than plain old WP, but it depends on what you're trying to accomplish.

          • realistdreamer Tuesday, September 8, 2009 at 5:48 pm

            Yeah, here's the basics.

            Imagine a bingo portal (It's not about bingo) using WP as a CMS.

            -Users get a directory of "every" site related to bingo.
            -Users get updated news from the bingo world.
            -Blogging about bingo

            No MU/BP needed yet, but:

            -Users can search for local bingo locations, rate them, "claim" them as their favorite spot.
            -Bingo consultants can get a free "page" (post template) and users can provide testimonials, ratings, reviews and claim the consultant as their own.
            -Users can post and share photos of their bingo exploits which could be associated with locations in the database.
            -Users can include their bingo skill in their profile (user meta) and search for other users to go play bingo with (using PM)
            ads can be served based on data in user meta.

            Bingo probably is a silly example, but it illustrates what I'm trying to accomplish. User meta will be important as will tracking user activity across the site. Hope this makes sense.

            Thanks for the replies.
            RD - A realist, but a dreamer at heart.

            • Daniel Quinn Tuesday, September 8, 2009 at 9:16 pm

              Yikes, that's ambitious. The latter stuff--especially putting together the location-based information--is going to require a lot of "extending" of WP, to say the least. I'm not sure if even BuddyPress + WPMU is going to be enough (that is, they won't be able to do what you want out-of-box, but I'm sure you know this already)

              • realistdreamer Tuesday, September 8, 2009 at 10:47 pm

                Yeah thanks. I've been scouring plugins for a long while and think I've got the list I need for most everything. Now I've got to check if I can get them to play together and on MU.

                FYI, there's a plugin that creates a Yahoo style directory, but it's $29 and not on Wordpress.org. There are also geocoding plugins based on ip address as well as a simple Google API for this and just about everything else. Who Sees Ads MU will serve ads and other "widgets" based on whatever PHP criteria I set. I'm going to need to play with the BP hooks to do the activity stuff I mentioned, but the existing activities should be a roadmap. Funny thing is I know how to accomplish these tasks without wordpress just using PHP/MySQL. My challenge will be doing it efficiently within the WP framework without breaking anything. I'll come back when I git- er-dun!

                Thanks again, I definitely think I'm going to struggle through MU.

                BTW, everyone should check out Google Code for a bunch of cool, simple to implement stuff like an off the shelf Calendar widget and more. http://code.google.com/more/#products-products-accounts

                • Daniel Quinn Tuesday, September 8, 2009 at 11:33 pm

                  Best of luck. Give me a heads up when you're diving into the code and have made some progress. I'm no programmer, so I can only imagine what will be involved for some of the more complex stuff. If you already know how to pull off the majority of want you want in PHP/MySQL already, I think it'll be a breeze for you, especially since WP does a lot of the heavy-lifting (and tedious database stuff) out-of-box. It's just a matter of fitting together the plugins in such a way that they don't all explode and create some horrifying FrankenWP.

                  You ought to blog/write about your experience ripping through WP after/during the process, because it'll really be interesting to see WP tricked out this much (if you can write about it discreetly, that is, since obviously you don't want to give away what the project is all about until it's ready to roll.)

                  • realistdreamer Wednesday, September 9, 2009 at 1:55 am

                    I've probably oversold my PHP/MySQL skills, but most of what I want is to do JOINs in DB tables to get data from one table (e.g. userid) to speak to data from another table (e.g. their favorite post or photo or author).

                    After I realized what I wanted to do was beyond the norm, I bought designOdyssey.org and will use that to tell of the journey. Right now it's hosted at designOdyssey.wordpress.com primarily so I can begin to familiarize myself with WP which has been fun. I'll move it over (Maybe as an MU blog) once I finish the planning stages and get deeper into WPMU.

                    I'll certainly repost that link when I get further in. Nothing of interest for your readers yet.

  6. Jen Wednesday, September 9, 2009 at 2:23 pm

    Thank God for this! Everything I needed without all the bloat of a crappy, obsolete plugin. I'm a happy camper! :)

    • Daniel Quinn Wednesday, September 9, 2009 at 3:10 pm

      yeah, bloated plugins give me headaches. Be sure to report back if you did something sweet for a client.

  7. Adam Kayce Friday, September 18, 2009 at 1:10 pm

    This is awesome; thanks for putting these tutorials out. You totally saved my bacon.

    I flailed around with Register Plus and CIMY before finding this, unfortunately... but I got this up and running easily, and it works like a charm.

    Thanks again.

  8. Jillian Madison Friday, September 18, 2009 at 11:10 pm

    Amazing, just what I was looking for. Will you marry me?

    • Daniel Quinn Friday, September 18, 2009 at 11:19 pm

      lol, if you teach me how to cook. ;)

  9. Jillian Madison Saturday, September 19, 2009 at 7:40 pm

    Again, stellar work! This saved me a ton of time. I'm really new to PHP, and have what might be a stupid question:

    Right now, you're using "input type="text" as an input field, and everything is working great. But I need a larger text input area so users can type longer answers.

    I tried incorporating TEXTAREA into the php, but for some reason, the information isn't being stored or updated. When a user goes back to re-edit the profile, the info they typed there is gone. Any idea why? Here's the code I'm using:

    <textarea rows="5" cols="5" name="extra3" id="extra3" value="" />

    (and yes, I did remember to update/process them!)

    :) Thanks in advance!

    • Aim Tuesday, September 29, 2009 at 3:56 pm

      Hi, textarea is a pair tag and can't be self-closed. It needs a closing tag and a value is between the start tag and the end tag:

      your text

      See http://www.w3schools.com/html/html_forms.asp

      • Daniel Quinn Tuesday, September 29, 2009 at 5:17 pm

        And see the example in the article, also -

  10. Jillian Madison Wednesday, September 23, 2009 at 11:29 pm

    IT'S ME AGAIN! :)

    I have another question! Is there a way to hide the field on the user's profile page if they haven't filled in any info for it? A few people haven't filled out the extra fields, and their profile page is displaying the questions anyway.

    You're my hero,
    Jillian

    • Daniel Quinn Wednesday, September 23, 2009 at 11:35 pm

      You'd want to do an if statement, something like:

      if ($curauth->something) {
      echo $curauth->something;
      }

      but be sure to include the markup (the dd in the example) in the if statement.

      • Jillian Madison Wednesday, September 23, 2009 at 11:41 pm

        Ah, you lost me at "something." I think that's too over my head. I'm insanely new to all of this. Gee, does it show? :)

        Thanks for responding anyway. -J

        • Daniel Quinn Thursday, September 24, 2009 at 10:36 am

          I've added more detailed instructions in the article -

  11. DC Monday, October 5, 2009 at 5:41 pm

    Barry, I'm having the exact same problem. The information that's been input into the textarea is not being saved when I click "update profile."

    Did you ever figure this out? I've been at it for 2 days and it's driving me crazy.

    • Daniel Quinn Monday, October 5, 2009 at 11:09 pm

      Are you using the code in the example above? Remember a textarea is not like other form fields in that it's not self-closing.

  12. Christina Tuesday, October 6, 2009 at 3:37 am

    Wow -- I think I love you. If you didn't already take Jillian up on her offer of marriage, she's about to have some competition.

    Thanks so much for this series!

  13. Rose Thursday, October 8, 2009 at 2:53 pm

    Hello, this plug-in is great. I'm figuring out how to add fields, but I don't see that information that gets entered on the profile page is being saved. I also don't quite understand what to do with the code in the explanation after the first block that I put in my plug-ins folder and activated.

    Can someone tell me what I've missed in regard to the info in the fields being actually saved with the user profile?

    Thanks!

    • Daniel Quinn Thursday, October 8, 2009 at 7:41 pm

      You might want to read the beginning of this series, which explains how to make an author template to echo out the profile field values.

      • Rose Wednesday, October 14, 2009 at 3:40 pm

        Ok, so I've been reading the directions over and over again trying to follow what they mean. I've been able to add fields to the profile page in the back-end but when I fill the form out it doesn't stick.

        I see that there is a part about adding to a definitions list and things showing up on the author page, but I'm not clear about where these definitions are put. On the author page template or in the my-custom-user-meta.php file?

        And do I change the ">something;" to ">myinputname;" What do I put there to make it see the fields I've added?

        Thanks for your help. :)

        • Daniel Quinn Wednesday, October 14, 2009 at 9:01 pm

          No worries Rose, we'll get you going. The definitions list (the last segment of code) is what goes in author.php in your /wp-content/themes/yourtheme/ directory. You'll need to create this file (see the previous post in the series for the full code for that template) and the segment of code would go wherever in that file you want it to appear. And yes, you can change "something" to the name of the variable you'd like (but make sure you change it everywhere it's used).

          • Rose Monday, October 19, 2009 at 11:40 pm

            Thank you so much for your help. I finally have the the author page set up. I plugged in the $curath codes for each field. I know they work because all the $curath for standard Wordpress fields show up like a charm.

            However, none of the fields I have added with the custom meta plug-in will echo what I've input into them. And I'm still having the same problem as before in the back-end. The information I enter into the profile fields do not save. They disappear when I click the "update profile" button.

            Aren't these supposed to be going into the database? How do they get in there? And how can I get things to work?

            Thanks again!

          • Rose Wednesday, October 21, 2009 at 2:41 am

            WOW! Here is another interesting observation. Some fields I added work and some don't. Why? But even more perplexing is that if I fill one out and update my profile it appears on the author page, but if I go back and fill out another (working) field and then update the profile it removes the one I already filled in and shows the new one on the author page. Do you have any thoughts on this?

            • Daniel Quinn Wednesday, October 21, 2009 at 7:28 am

              Hi Rose, could you post the code you're using as the plugin? It sounds like there's something wrong with your fields.

              • Rose Wednesday, October 21, 2009 at 2:30 pm

                Maybe the comments don't like my code. My reply doesn't seem to be showing up. Is there another way I can send it to you?

                • Daniel Quinn Thursday, October 22, 2009 at 8:50 am

                  Hi Rose - I just installed a syntax highlighter for my comments. Enclose your code in php braces (see the comment form itself for an example) and you will get something like this:

                  <?php
                  echo "My test link.";
                  ?>
                  
                  • Rose Thursday, October 22, 2009 at 2:42 pm

                    I've tried 3 times and the comment never shows up. I'm starting to think I'm internet code challenged. The code is very long the idea is to make each profile into a resume (with info from the job application) for hiring within the company. I'll try to see if it's just too long by posting a bit of us.

                  • Rose Thursday, October 22, 2009 at 2:46 pm

                    This is just the first part. I've pulled out the first section to see if a smaller amount of code would post.

                    
                            
                    
                    
                    
                                <input type="radio" name="abilityToMultitask" value="" id="abilityToMultitask_1">
                    
                              <input type="radio" name="abilityToMultitask" value="" id="abilityToMultitask_2">
                    
                              <input type="radio" name="abilityToMultitask" value="" id="abilityToMultitask_3">
                    
                              <input type="radio" name="abilityToMultitask" value="" id="abilityToMultitask_4">
                    
                              <input type="radio" name="abilityToMultitask" value="" id="abilityToMultitask_5">
                    
                  • Rose Thursday, October 22, 2009 at 2:47 pm

                    Something went wrong. It's not even all there! I can't delete the comment either. I'm so sorry. This is getting flustering. I'll try again.

                    • Rose Thursday, October 22, 2009 at 2:59 pm

                      I tried to post the code again and now the comment isn't here. This is not working. Can I e-mail it to you? Maybe I could give you a link to download the my-custom-user-meta.php file? And then adding to my fluster the comments form won't let me insert my cursor in between text without highlighting the whole field. I'm so sorry for being so utterly useless...

                    • Daniel Quinn Thursday, October 22, 2009 at 7:47 pm

                      Can you upload your file to your server as a .txt file and post a URL here? Then I can view it.

                    • Rose Thursday, October 22, 2009 at 9:02 pm

                      http://www.snuba.com/customMeta/my-custom-user-meta.txt

  14. Daniel Quinn Friday, October 23, 2009 at 1:07 pm

    You're going to scream when you hear this: Apparently WordPress won't save the variables if they are camel-case. So "oceanPhoto" won't save, but "oceanphoto" will. Go through your plugin script and change all variables to lowercase and it will work.

    • Rose Friday, October 23, 2009 at 4:15 pm

      YES! That was tedious but it worked perfectly! You are AWESOME! Thank you, Thank you, Thank you!!!

      I hope I might also be able to ask another question. How do I correctly format radio buttons and checks? Those are the only fields that are still not working/not showing up/saving and think I may have something set up wrong with them. I put a new updated version of the file at the link I gave above.

    • Rose Friday, October 23, 2009 at 9:20 pm

      Oh dear, and I almost forgot, drop down selections too.

      • Al Monday, October 26, 2009 at 1:44 am

        Yes its true, if you make the variables uppercase it does not store the value! Lowercase works fine.

        Thanks for the tut! Implemented it perfectly!

      • Daniel Quinn Monday, October 26, 2009 at 8:45 am

        Before I check this out, could you verify - 1) Have you tried to echo the value out in your author template (in which case it is actually saving it to the database) and 2) Have you tried to conditionally check if the value echoed out is equal to the dropdown value and then place a "selected" (or "checked=true" for radio buttons/checks) attribute on the option group?

        • Rose Monday, October 26, 2009 at 1:34 pm

          I guess my problem is a bit more inexperienced than that. I don't know how to properly echo the value. I have a value echoed in my author template for the radio buttons/checks/drop downs but because each one has multiple values I'm confused.

          I'll explain: With text fields and text area fields it was easy. The name, id and value (after "get_author_meta" between the single quotes) were the same value:

          [/php]
          
          I could repeat that value in the process code at the bottom like this:
          
          [php]update_usermeta($user_id, 'value', ( isset($_POST['value']) ? $_POST['value'] : '' ) );[php/]
          
          but with radio buttons it's a whole new ball of wax. There's the name, id and value however each button in the group has a different value and id. Or does it? Isn't the value the meaning of the button? For example if there are two buttons and one represents "yes" and the other "no" then isn't the value for one yes and the other no? And then with the processing code, do I process them as a radio group and use the name/id/value for the group, or do I have to have something for each value and if so how do I echo that in the author template so that it shows yes or no for that particular question depending upon what the user filled out? Right now I just have it echoing the value for the group and all the values within the group are the same but the id is different:
          
          [php]<input type="radio" name="yesorno" value="" id="yesorno_1"><input type="radio" name="yesorno" value="" id="yesorno_2">

          Then I process it with:

           function action_process_option_update() {
              global $user_id;
              update_usermeta($user_id, 'yesorno', ( isset($_POST['yesorno']) ? $_POST['yesorno'] : '' ) );
          

          And then echo it with:

          yesorno; ?>

          I'm certain this is wrong but I'm sufficiently confused enough that I'm not sure how to handle it.

          • Rose Monday, October 26, 2009 at 1:35 pm

            Sorry the php thingy got really screwed up.

          • Rose Monday, October 26, 2009 at 1:44 pm

            Let's try again:

            I guess my problem is a bit more inexperienced than that. I don't know how to properly echo the value. I have a value echoed in my author template for the radio buttons/checks/drop downs but because each one has multiple values I'm confused.

            I'll explain: With text fields and text area fields it was easy. The name, id and value (after "get_author_meta" between the single quotes) were the same value:

            [/php]
            
            I could repeat that value in the process code at the bottom like this:
            
            [php]update_usermeta($user_id, 'value', ( isset($_POST['value']) ? $_POST['value'] : '' ) );

            but with radio buttons it's a whole new ball of wax. There's the name, id and value however each button in the group has a different value and id. Or does it? Isn't the value the meaning of the button? For example if there are two buttons and one represents "yes" and the other "no" then isn't the value for one yes and the other no? And then with the processing code, do I process them as a radio group and use the name/id/value for the group, or do I have to have something for each value and if so how do I echo that in the author template so that it shows yes or no for that particular question depending upon what the user filled out? Right now I just have it echoing the value for the group and all the values within the group are the same but the id is different:

            <input type="radio" name="yesorno" value="" id="yesorno_1"><input type="radio" name="yesorno" value="" id="yesorno_2">

            Then I process it with:

             function action_process_option_update() {
                global $user_id;
                update_usermeta($user_id, 'yesorno', ( isset($_POST['yesorno']) ? $_POST['yesorno'] : '' ) );
            

            And then echo it with:

            yesorno; ?>

            I'm certain this is wrong but I'm sufficiently confused enough that I'm not sure how to handle it.

            • Daniel Quinn Tuesday, October 27, 2009 at 9:34 am

              I've added an explanation as to how one would handle checkboxes, radio buttons, and dropdowns to the tutorial.

              • Winegoddess Thursday, October 29, 2009 at 12:23 am

                THANK YOU!

                You have taught me so much in this one article!

                I am having issues though.

                Logged in as a subscriber, Im unable to see my own extra field.

                My value is saved in the db - took me an hour to see it - on page 2 of PhpMyadmin results...ugh.

                AND - I can see it when I am logged in as an admin looking at the subscriber.

                Any ideas? Thanks!

                Judy

                • Daniel Quinn Thursday, October 29, 2009 at 10:17 am

                  Do you mean on the page that uses the author template, or in the profile?

                  • Winegoddess Thursday, October 29, 2009 at 10:18 am

                    Thanks for your reply! I mean when the user views their own profile. Im using the Customize Your Community plugin. It calls do_action('show_user_profile');

                    This displays the new extra fields, but is not showing values.

                    I added debugging code on your AWESOME PIECE OF WORK, and it does not have access to $user_id.

                    However, it does save to the db somehow.

                    A thousand thank you's and a kajillion units of good karma if you can help. :)

                • Winegoddess Thursday, October 29, 2009 at 12:00 pm

                  I solved it by adding this to the plugin. Not sure if it is the most elegant solution.

                  function action_show_user_profile() {
                      global $user_id;
                  
                      if (!$user_id) {
                      	$current_user = wp_get_current_user();
                  		//global $user_id;
                  		$user_id = $current_user->ID;
                  
                      }
                  
                  • Daniel Quinn Thursday, October 29, 2009 at 12:06 pm

                    Fascinating. I was just about to take a look at this, as CYC is something I too want to use in the near future. Do all the extra fields save/function as expected?

  15. Lisa Sunday, October 25, 2009 at 11:04 am

    Hi Daniel,

    Thanks for this great tutorial! One thing I don't know how to do is to get the extra fields to display on separate lines. Right now its displaying in sentence structure. Is this possible to do?

    • Lisa Sunday, October 25, 2009 at 12:47 pm

      solved, added break tags on the Members List - Configure Markup page.

  16. Lisa Sunday, October 25, 2009 at 12:40 pm

    Hi again,
    It seems that this doesn't work with the "Theme my Profile" plugin. Any suggestions?

  17. Vladart Sunday, October 25, 2009 at 7:10 pm

    Hey, this is a great, lets call it a plugin, but what about checkbox inputs? I've tried to make it work, but... The checkbox isn't staying checked after the form being submitted and ofcourse the value of it isn't showing up on a page. If any one knows the solution, please share.

    • Daniel Quinn Monday, October 26, 2009 at 8:42 am

      Before I check this out, could you verify - 1) Have you tried to echo the value out in your author template (in which case it is actually saving it to the database) and 2) Have you tried to conditionally check if the value echoed out is equal to the checkbox value and then place a "checked=true" on the checkbox?

      • Vladart Monday, October 26, 2009 at 10:06 am

        I've tried to echo it and it did show up in somepoint... Yes, if put checked="checked" in the input tag, for a test purpose, it's working ok.

        • Daniel Quinn Tuesday, October 27, 2009 at 9:35 am

          I've put an example in the tutorial above.

          • Vladart Tuesday, October 27, 2009 at 5:59 pm

            It's great and it's working! Thanks very much for your help. There is one little problem. In case the checkbox is not checked there is a PHP warning: Warning: in_array() expects parameter 2 to be array, string given in...

            • Daniel Quinn Tuesday, October 27, 2009 at 9:20 pm

              My mistake, I didn't check if the checkbox values were arrays before echoing them. I've added a check to is_array to fix this.

              • Vladart Wednesday, October 28, 2009 at 4:11 am

                Daniel, thanks for creating this very nice plugin and for your help. I'll send you a link, after i'll finish doing what i'm doing.

  18. jinglicio.us Thursday, October 29, 2009 at 2:40 pm

    Daniel, thanks for the great tutorial and starter code.

    I was curious how much I would need to change/add in order to display a set of custom meta fields as a stand alone form within a post?

    • Daniel Quinn Thursday, October 29, 2009 at 5:45 pm

      By custom meta fields, do you mean profile meta, as in the information that users can enter into their profiles, or custom meta as in the information that can be assigned to any post via custom fields?

      • jinglicio.us Thursday, October 29, 2009 at 7:20 pm

        I was thinking the profile meta, as in the information that users can enter into their profiles...

        So the form would be public (tied to a post), but they could change it later via their profile.

        • Daniel Quinn Thursday, October 29, 2009 at 7:45 pm

          Hmm, that is an interesting question. I'm not sure why you want to tie it to a post, but I could see how this would work with a page. If you had a custom page template, you could have it present a form to update profile meta only if the user is logged in (otherwise it asks the user to log in). Then you could check which user is logged in and populate the form based on that. The only difficulty is that I think you'll have to interact directly with the database to update the user's information, and since I'm not well-versed in how WP's security as far as writing directly to the database goes, I can only advise you to be careful:

          if (is_user_logged_in()) {
               // get the database query
               global $wpdb;
               // 1. Figure out which user it is by comparing the user ID or some other unique identifier to the ID in the database.
               // 2. Use $wpdb->get_var("SQL goes here"); to retrieve the profile meta and populate the form for that user.
               // 4. Create your own validation (both front-end and server-side) and then hope it works.
               // 3. Use $wpdb->query($insert); (where $insert is the altered profile meta) to insert the data back.
          }
          
  19. Ruben Tuesday, November 3, 2009 at 6:07 am

    Thank you for this nice tutorial. I was using the plugin 'Extended User Profile', but this was not very flexible. Changing this plugin is easy and suitable for me.

    A question I have regarding this is the following. How can I list all the users sorted on one of the custom user meta fields?

    I use the following query to retrieve all the users/members, but in that one I can only sort on: ID, user_login, user_nicename, user_email, user_url and user_registered and not on the userdata fields.

    $szSort = "user_nicename";
    
    $aUsersID = $wpdb->get_col( $wpdb->prepare(
    	"SELECT $wpdb->users.ID FROM $wpdb->users ORDER BY %s ASC"
    	, $szSort ));
    
    foreach ( $aUsersID as $iUserID ) :
    	$user = get_userdata( $iUserID );
    	echo ucwords( strtolower( $user->first_name ) );
    endforeach;
    
    • Daniel Quinn Monday, November 9, 2009 at 12:14 pm

      Can anyone help out Ruben? I haven't done a lot of work with direct database queries, but sorting users by a custom field would be very useful.

  20. Alvin Crespo Wednesday, November 4, 2009 at 8:44 pm

    Hey Everyone,

    Great Post! However I am trying to add the first name and last name fields onto the signup/register form? Any advice? I can't seem to find any good advice or reference for this. Any help would be great, thanks!

    • Daniel Quinn Monday, November 9, 2009 at 12:10 pm

      That is a good question, as in this setup we want to use the custom fields we've added rather than fields that might be added through a plugin like Register Plus. I will be working on a site soon that requires an extended registration form, so I will get back to you if I come up with a solution.

  21. enclave Monday, November 9, 2009 at 4:02 am

    Thanks for sharing.

    I am hoping one of the approaches here is going to enable me to do the personalization of pages i am looking at with user meta data

  22. Barney Tuesday, November 10, 2009 at 12:57 am

    great plugin/tutorial!
    I got it to work on the author.php page,

    Is there any way I can get the fields to show up on a post page like the single.php page

    ie at the end of each post I want some of the extra author info to show.

    Thanks!
    B

    • Daniel Quinn Tuesday, November 10, 2009 at 6:01 pm

      You should be able to make a call to the_author_meta because these values are saved in the same place that regular author meta is saved:

      echo "This is my Twitter name:";
      the_author_meta('twitter');
      
  23. Giles Tuesday, November 10, 2009 at 5:07 pm

    Hi,

    Thanks for this tutorial, its really helped me customise my blog the way i need to.

    I was wondering though and
    hopefully i didnt miss this but is there was anyway to only show users with certain roles such as just showing contributors etc or even better custom roles that are created with another plugin?

    Thanks

    Giles

    • Daniel Quinn Tuesday, November 10, 2009 at 5:56 pm

      I think you'll need to modify Members List so that the plugin only gets users where the user_level is equal to the role you are interested in. Last I remember, Members List makes direct calls to the database to compile the list of users.

  24. Vlad Tuesday, November 10, 2009 at 5:31 pm

    Hey Daniel, if you remember i was asking you about the checkboxes, and i wrote that i will show you the result of the work. So here it is http://prospace.superfreelancer.com. "Search freelancers by skills" - on the right is actually the values of checkboxes of custom user meta script. Register a profile if you like, and check how it's work. Thanks again for creating this script.

    • Daniel Quinn Tuesday, November 10, 2009 at 5:52 pm

      This looks fantastic Vlad, great job. I signed up to check it out (you can delete me if need be!). I also like that you have the user registration in the theme's design, that is a nice touch.

  25. Giles Tuesday, November 10, 2009 at 5:58 pm

    Hi Daniel,

    Thanks for the quick response.

    I will have a look into it and if I get anywhere will share the results with everyone else.

    Thanks again mate

    Giles

  26. Timur Saturday, November 14, 2009 at 12:45 pm

    Hello, thank for helpfull article.
    How can i add fields editign only by admin?

    • Daniel Quinn Saturday, November 21, 2009 at 3:48 pm

      In the plugin script, see if you can check if the logged in user has a high enough user level via an if statement. Enclose the editable field in this conditional.

  27. Ruben Saturday, November 21, 2009 at 9:29 am

    I solved the problem mentioned earlier regarding the sorting of the custom fields. In the following blogpost (in Dutch) you can see the solution I had implemented: http://rubenwoudsma.nl/wordpress-gebruikersprofiel-uitbreiden/

    • Daniel Quinn Saturday, November 21, 2009 at 3:49 pm

      Great! Thanks for sharing. I will update the article to include your link.

  28. Debbie Campbell Tuesday, November 24, 2009 at 1:00 pm

    This is a great guide - I'm on the last post and everything's working fine. I'm not a programmer but I've had no problems implementing this excellent tutorial.

    I'd tried RegisterPlus and CIMY but wasn't happy with either - this seems to be perfect.

    One potentially dumb question: Is it possible to show the new profile fields in the registration form? wp-login.php?action=register... Or do I make my users register and *then* go to the profile form?

    And, are these 'regular' custom fields so that I can do anything with them that I'd normally do with custom fields, like sorting?

    • Daniel Quinn Tuesday, November 24, 2009 at 1:47 pm

      Sadly, I don't know how you would show the new profile meta on the registration form. There are plugins however that redirect the user to a specified page (or the profile) upon login/registration, so making them fill out the profile after they register is a possibility. Moreover, it's probably better from a user experience perspective to have them just fill out their basic credentials (no extra meta), as that's what most other sites do these days.

      These fields are "regular" in the sense that they're stored and managed in the database the same way the default fields for the profile are (no wonky extra tables or whatnot), but they are not custom fields (so you can't use get post custom meta to retrieve them), because custom fields are meta assigned to posts. Because these are author meta, you can use the default author meta functions to retrieve them in the loop.

      (*edit* as for sorting, see Ruben's comment in this thread. He posted a solution on his blog, but you'll need to use Google Translate unless you speak Dutch.)

      • Debbie Campbell Tuesday, November 24, 2009 at 1:59 pm

        Thanks for the quick reply! It's a directory for designers so I can just modify the new user email to let them know they need to edit their profile. Thanks again.

      • Ian Tasker Sunday, November 29, 2009 at 7:54 am

        @Daniel,

        I have been working on extending the registration form with wordpressmu.

        if you which to add fields to the form you put the html in a function and call it by
        add_action('signup_extra_fields', 'registration_form', 1);
        to validate it by using
        add_filter('wpmu_validate_user_signup', 'registration_validation');

        the current bit i'm working on is adding all the information to user meta data, once i have go this working i will post abut it.

        • Daniel Quinn Sunday, November 29, 2009 at 9:43 pm

          Hi Ian - that sounds awesome. Is there a comparable function for add_filter('wpmu_validate_user_signup','registration_validation'); in regular WP?

          Look forward to reading your post.

  29. Richard Tape Friday, November 27, 2009 at 9:31 am

    Hey! Thanks for this brilliant tutorial! It's already helped me add to the user's profile page. (I too had found the ftwr code however I couldn't get it to work it checkboxes - you should me how, thank you!)

    I do have one question though. Do you know where I can look to be able to add one of my bits of custom meta to the users.php page - i.e. the page which lists the users? I'd like to add another column after "posts" which includes the new checkbox that I've implemented. Is this possible? Got any pointers?

    Again, thanks for this! From a blustery, chilly, rainy Manchester, England.

    • Daniel Quinn Saturday, November 28, 2009 at 3:19 pm

      I think you'd have to modify the Members List plugin. The key may lie in the function called pf_get_vars() in the plugin, because this is where he gets the user's profile information, and so your custom profile data would be obtainable there.

      You would just need to retrieve the fields you want in that function, and then maybe set them as global variables so you can access them in the pf_output_directory() function.

  30. westi Friday, November 27, 2009 at 3:45 pm

    By the way you might want to update these examples as I have updated the original to fix an issue it could have with its reliance on the $user_id global

    • Daniel Quinn Saturday, November 28, 2009 at 3:10 pm

      Thanks for the heads up - I updated the examples here to match the fixes you made.

      Thanks again for providing the plugin for us! Definitely a great holdover until 2.9 comes out.

  31. Luis Tuesday, December 1, 2009 at 1:05 am

    hi,

    I did the trick, I see the fields on the admin back end profile page for users but when I hit save/update it wipes the field values away and they cannot be seen any longer. I doubt they get saved or stored.

    help, appreciated.

    • Daniel Quinn Tuesday, December 1, 2009 at 8:54 am

      Are your variables all lowercase? Are you processing the variables at the end of the script?

      • Luis Tuesday, December 1, 2009 at 10:51 am

        yes I think so, please check below:

        <input type="text" name="extra1" id="extra1" value="ID) ); ?>" />

        <input type="text" name="extra2" id="extra2" value="ID) ); ?>" />

        <input type="text" name="extra3" id="extra3" value="ID) ); ?>" />

        ID, 'extra1', ( isset($_POST['extra1']) ? $_POST['extra1'] : '' ) );
        update_usermeta($user->ID, 'extra2', ( isset($_POST['extra2']) ? $_POST['extra2'] : '' ) );
        update_usermeta($user->ID, 'extra3', ( isset($_POST['extra3']) ? $_POST['extra3'] : '' ) );
        }

        }

        /* Initialise outselves */
        add_action('plugins_loaded', create_function('','global $custom_user_meta_instance; $custom_user_meta_instance = new custom_user_meta();'));
        ?>

        • Luis Tuesday, December 1, 2009 at 10:52 am


          /*
          Plugin Name: Custom User Meta
          Plugin URI: http://blog.ftwr.co.uk/archives/2009/07/19/adding-extra-user-meta-fields
          Description: Allows users to configure extra user meta values.
          Author: Peter Westwood, Daniel Quinn
          Version: 0.02
          Author URI: http://www.dquinn.net

          Add new custom user meta fields to the user's Profile in the WordPress Dashboard.

          */

          class custom_user_meta {

          function custom_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() {
          ?>

          <input type="text" name="extra1" id="extra1" value="ID) ); ?>" />

          <input type="text" name="extra2" id="extra2" value="ID) ); ?>" />

          <input type="text" name="extra3" id="extra3" value="ID) ); ?>" />

          ID, 'extra1', ( isset($_POST['extra1']) ? $_POST['extra1'] : '' ) );
          update_usermeta($user->ID, 'extra2', ( isset($_POST['extra2']) ? $_POST['extra2'] : '' ) );
          update_usermeta($user->ID, 'extra3', ( isset($_POST['extra3']) ? $_POST['extra3'] : '' ) );
          }

          }

          /* Initialise outselves */
          add_action('plugins_loaded', create_function('','global $custom_user_meta_instance; $custom_user_meta_instance = new custom_user_meta();'));

        • Daniel Quinn Tuesday, December 1, 2009 at 12:07 pm

          Can you upload a .txt file containing your code to your server, and supply the link here?

          • Luis Tuesday, December 1, 2009 at 4:52 pm

            here is the link

            http://www.craftitonline.com/wp-content/plugins/my-custom-user-meta.php.txt

            I thought that the reason that it was not saving the values for extra1 extra2 and extra3 was that they were not created in the database? But I did not see that step in the post. You also mentioned the use of register plus, but even when I install that and create those fields, this thing would not make sense for me.

            Any ideas?

            • Luis Tuesday, December 1, 2009 at 8:43 pm

              I am really amazed at how it can work in other people's wordpress setups when there is no actual save of the profile-key and value to wpdb...

              I just found another plugin for extending the profile however, it does it all with a single variable uep_meta which is not what I want.

              Any ideas?

              • Daniel Quinn Tuesday, December 1, 2009 at 9:19 pm

                Hi Luis, your frustrations are warranted: it's not your fault. When Peter updated the original script (see my last update to this article), he noted that $user_id is now replaced with $user->ID. When I updated my script in this tutorial, I left out two key variables he added.

                Go ahead and copy the plugin from this page again, and it will work.

            • Daniel Quinn Tuesday, December 1, 2009 at 9:20 pm

              No need for Register Plus - see my comment above. :)

              • luis Friday, December 11, 2009 at 12:05 am

                it worked! thanks.

  32. Debbie Campbell Tuesday, December 1, 2009 at 11:53 am

    The custom plugin is working beautifully. I'm also so close to being able to display a list of members by industry - I have one question though about displaying things in the loop.

    Here's my code snippet for displaying a member's company name according to their industry (industry and company are two new meta fields I created using this plugin):

    user_id);
    if ($user->industry='Photography') {
    echo "$user->company";
    }
    }
    }
    else { echo "Your blog appears to have no users..."; }
    ?>

    This is very close to working in that it does correctly pull all the members out of the db and print the author meta 'company' field's value. But it's printing them for everyone and I want to restrict it by the value of the author meta 'industry' field - when 'industry' = 'something', print 'company.'

    Any suggestions? I had no idea this would be so hard...

  33. Debbie Campbell Tuesday, December 1, 2009 at 11:55 am

    Sorry - let me try that without the php tags:

    $blogusers = get_users_of_blog(); //gets registered users
    if ($blogusers) {
    foreach ($blogusers as $bloguser) {
    $user = get_userdata($bloguser->user_id);
    if ($user->industry='Photography') { echo "$user->company"; }
    }
    }
    else { echo "Bizarrely, your blog appears to have no users. Something weird has happened."; }

    • Daniel Quinn Tuesday, December 1, 2009 at 12:10 pm

      I think it's because you're not using the right equals sign. It needs to be two equals "==" instead of just "=" equals. The code should be:


      if ($user->industry == "Photography") {

      echo "$user->company";

      }

  34. Debbie Campbell Tuesday, December 1, 2009 at 12:49 pm

    The two == signs were it! Thank you so much, I've been trying to get this to work for days. I'll post a link to my directory once it launches.

  35. Debbie Campbell Thursday, December 10, 2009 at 11:12 pm

    I'm so close to finishing my directory site. I have one major issue left - when a user fills out a profile after registering and submits, all the custom meta data submitted goes blank in profile.php. It *does* show up on the MembersList page so I know it's in the database, but it's not shown in the profile.

    I *can* however view the custom meta in the admin backend, like when I edit a member profile in the MembersList section of the backend. But not in profile.php.

    I think this is because of ilne 17 in the proof-of-concept snippet in step 5 - 'if ( is_admin() ) {'

    I'm using the plugin WP Hide Dashboard to keep subscribers (members) from accessing my admin area. Is this why the custom user fields, limited by is_admin, are showing up as blanks for subscriber-level members? And if so, is there a conditional way to say 'if this is profile.php then display the custom user fields as entered' in my-custom-user-meta.php's functions?

  36. Debbie Campbell Friday, December 11, 2009 at 3:45 pm

    Sorry for posting again - I'm so close to getting this working.

    I'm having some of the same issues others noted - custom user meta doesn't display in the profile – once the user enters it, it's being saved (because it shows up in the author template correctly) but isn't being displayed on the profile page that members have access to.

    All the custom fields and labels are there, but they’re all blank. And if the user saves that blank profile, it writes over all saved information.

    However, when I'm logged in as administrator and look at anyone’s profile, *all* the data is displayed correctly.

    I removed the WP Hide Dashboard plugin thinking that might be the problem, but there wasn't any change.

    I was hoping I was having the same problem that Rose was discussing in comments around Oct. 23, but no… Can you look at my .txt file and see if there's some terribly obvious problem with it?

    http://www.redkitecreative.com/projects/cr/my-custom-user-meta.txt

  37. Debbie Campbell Tuesday, December 15, 2009 at 5:40 pm

    Is there any kind of permissions setting I need to edit in order to be able to display entered custom author meta in the profile when a non-admin is viewing it? Extra profile data only displays for admins, not for subscribers.

    • Daniel Quinn Tuesday, December 15, 2009 at 5:54 pm

      Hi Debbie, sorry I've been busy. As you can see at my dev site (the example site in the tutorial), if you log in as a subscriber and save/edit custom user meta, the data is indeed saved and displayed to users who are not admins.

      If the data's being saved in your database, that means that there's something wrong with the script that echoes it back to the front end. To troubleshoot, reduce your script to just 1 or 2 custom values and turn off all your plugins.

      If it still doesn't work, please post a link to the script with just those two values so we can see where the logic is wrong.

      • Daniel Quinn Tuesday, December 15, 2009 at 5:57 pm

        A quick glance at your script: get_the_author_meta('portfolio6_url', $user_id) ); needs to be get_the_author_meta('portfolio6_url', $user->ID) ); but it's difficult to look at your script because there are so many values.

  38. Debbie Campbell Tuesday, December 15, 2009 at 6:03 pm

    Thanks so much - I will do as you suggest and reduce it to 1 or 2 variables and test.

  39. Debbie Campbell Thursday, December 17, 2009 at 1:13 am

    Thank you so much for your suggestions - I reduced the custom-usermeta plugin to one variable and went through all other plugins one by one until I found the problem - Customize Your Community. Now I just need to find another way to have the registration and login forms on template pages and I should be done with the directory. Thanks again.

    • Daniel Quinn Thursday, December 17, 2009 at 12:24 pm

      Ah, yeah another user had the same problem with CYC above. I believe she came up with a solution, so you might want to take a look at that.

      • Debbie Campbell Thursday, December 17, 2009 at 1:29 pm

        I tried using Winegoddess' suggestion and inserted that code snippet into CYC - it isn't working, though. I tried a few variations and still no results - I'll try to contact her directly and if I get it working, will post the code here.

  40. Shawn McGuire Thursday, December 17, 2009 at 12:15 pm

    I created the file my-custom-user-meta.php and edited it to reflect the field that I want. Where do I add the definition list? Also how would I go about calling that field in a page? I'm using exec-php so I can use php in pages and I am able to echo the users info but not the extra fields. I'm using the Atahualpa theme if that makes a difference

    • Daniel Quinn Thursday, December 17, 2009 at 12:25 pm

      Hi Shawn, have you read the other articles in the series? The definition list gets called in your author template (or potentially any template that requests the author meta). This should be done in actual templates, not via Exec-PHP in the Dashboard.

  41. Jocelyn Monday, December 21, 2009 at 5:38 am

    This approach works perfectly by when I try to use "theme my profile" for some reason the custom field does not update. Is there something special I need to do?

    Thanks

    • Daniel Quinn Sunday, January 3, 2010 at 10:47 am

      I don't know what Theme My Profile does programatically but you might try using WP 2.9's new functions for adding custom user meta and see if it works, since that's a more appropriate way to accomplish this now.

  42. Justin Saturday, January 2, 2010 at 7:11 pm

    Hi,

    I've been struggling with the checkboxes for more than 2 hours now.

    In your tutorial, it doesn't explain how to use the "update_usermeta" for the checkboxes, and that's exactly what I want to do.

    I've tried using (in update_usermeta) the strings "potatoes[]", "potatoes", renaming everything, etc... nothing works.

    Any help would be greatly appreciated.

    Thank you.

    • Daniel Quinn Sunday, January 3, 2010 at 10:45 am

      Hi, it's possible that the portion of the tutorial above showing how to enter the checkboxes was using $user_id instead of $user->ID, because I had updated the tutorial to match Peter's changes to the original. I double checked against my demo at dev.dquinn.net and changed that variable, so try that.

      The checkbox usermeta is no different than other usermeta:
      update_usermeta($user_id, 'penguins', ( isset($_POST['penguins']) ? $_POST['penguins'] : '' ) );

      • Justin Wednesday, January 6, 2010 at 7:49 am

        I figured it out 10 minutes after posting this, but I still thank you for replying ;-)

        And thank you(!) for making this tutorial. After Googling for a while, it seems that this is the best tutorial out there that teaches how to add custom user meta in Wordpress' profiles. So here's an Internet high five: http://www.ihighfive.com/

        Thank you.

        • Daniel Quinn Thursday, January 7, 2010 at 5:40 pm

          lol, thanks. Be sure to check out this link:

          http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields

          At Justin Adlock's blog, because he has a great tutorial about adding custom usermeta in 2.9 as we move toward 3.0.

  43. Jayson Friday, January 8, 2010 at 2:01 pm

    You are truly giving. I think you spent more time advising folks then writing the plugin itself! A true definition of your character. Ok DQ (did people call you that in 7th grade, ya know like daitry queen)?

    Never mind, so...
    1.) Do you know about the changes for 2.9 and the contactmethods filter?


    //Filter Profile Fields
    function my_new_contactmethods( $contactmethods ) {
    // add twitter
    $contactmethods['twitter'] = 'Twitter';
    //add Facebook
    $contactmethods['facebook'] = 'Facebook';

    return $contactmethods;
    }
    add_filter('user_contactmethods','my_new_contactmethods',10,1);

    2.) Need to add a file upload feature for .docs and .pdfs to the user profile.

    Been banging out code but can not get wp to store the file name nor upload the file to a directory.

    Whatcha think?

    PS: Check out wpcoop.org

    Thanks Daniel Quinn,

    Jayson

    • Daniel Quinn Friday, January 8, 2010 at 2:16 pm

      thanks, though it can be argued that being giving is also a character flaw. Yes to "DQ," as well. Don't forget Dr. Quinn, medicine woman.

      I note at the top of this tutorial that the new 2.9 functions are better for customizing the usermeta, but I haven't personally used them yet. I wonder if it's flexible enough to allow you to write out your own markup for the new fields? You look like you're ahead on that, so your guess is better than mine.

  44. Emi Wednesday, January 27, 2010 at 2:13 am

    Thanks you so much for this plugin. It works beautifully.

    However, one issue. I tired your radio button version and have registrators choose what 'gender' they are. It works perfect on profile pages. Now I would like it to automatically show that on each comments. Comment section are only for registered people. I did.....

    For some reason, if admin choose "girl" every single people's gender goes "girl". I wonder if this is something to do with plugin conflicts. Thank you so much for all your help.

    • Emi Wednesday, January 27, 2010 at 2:14 am

      I am sorry I missed the code brackets....

    • Daniel Quinn Wednesday, February 10, 2010 at 10:28 am

      You may have to reach into the database to do that.

  45. Radek Wednesday, February 3, 2010 at 9:10 pm

    Hi Daniel, thank you for the code. I was wondering if I can use your code or some of it to "add new custom user meta field" to all existing users and also to all new ones and then let only admin to edit the field for every single user using dashboard. I've been looking around and I cannot find any solution to that... Could you give me some hints?I do not even need to display that field anywhere.I need to used it in a condition.Let's say I want to display different ulr for users with that field == 'true'.Thank you.Radek

    • Daniel Quinn Wednesday, February 10, 2010 at 10:29 am

      Try to see if you can enclose the field in code that only displays if the currently logged in user has administrative privileges.

      • Radek Thursday, March 18, 2010 at 1:24 am

        yes, that's the trick. Something like that is the code I was looking for

        • Radek Thursday, March 18, 2010 at 1:25 am

          one more time. I won't post the full so it can be displayed .... if ( !current_user_can( 'manage_options'))

  46. Al Saturday, March 13, 2010 at 5:26 pm

    The Custom user meta plugin does not work on IIS server! Can you please update it to work somehow! I really need it!

    thanks!

    • Daniel Quinn Monday, March 15, 2010 at 3:13 pm

      Sorry, I don't work on IIS servers. If it doesn't work on IIS, you'll have to troubleshoot and figure out what IIS doesn't like about it.

  47. Anne Thursday, March 18, 2010 at 11:18 pm

    After reading through your excellent tutorials and everyone's comments, I finally got everything working except for the checkboxes. They work fine on the user admin page, but the output on the author page only shows the title of the list, followed by the word 'array' and none of the boxes checked by the user. I've tried everything, and also used the 'potatoes' script verbatim, with the same results.

    Here is some of the code I have used to define the checkboxes:
    [

    ID);
    ?>

    <input value="ola" id="ola" name="spiritual_profiles[]" checked="checked" type="checkbox" /> The OLA Profile
    <input value="1001" id="1001" name="spiritual_profiles[]" checked="checked" type="checkbox" /> 1001 Spiritual Orientation Profile]

    Here is the code I have put in the plugin to process the list:
    [ update_usermeta($user_id, 'spiritual_profiles', ( isset($_POST['spiritual_profiles']) ? $_POST['spiritual_profiles'] : '' ) );]

    Here is the code I have in the dl of my theme's author template file:
    [spiritual_profiles) { ?>
    ALC Certified to Administer the Following Spiritual Profiles
    spiritual_profiles; ?>
    ]

    What am I doing wrong?

    Thanks for all of your hard work! I'm using WP 2.9.1 with the Atahualpa theme.

    • Anne Thursday, March 18, 2010 at 11:24 pm

      Sorry, I'm new to all of this and my code didn't show up properly. I'll try again...

      Here is some of the code I have used to define the checkboxes:

      ID);
      ?>

      <input value="ola" id="ola" name="spiritual_profiles[]" checked="checked" type="checkbox" /> The OLA Profile
      <input value="1001" id="1001" name="spiritual_profiles[]" checked="checked" type="checkbox" /> 1001 Spiritual Orientation Profile

      Here is the code I have put in the plugin to process the list:
      update_usermeta($user_id, 'spiritual_profiles', ( isset($_POST['spiritual_profiles']) ? $_POST['spiritual_profiles'] : '' ) );

      Here is the code I have in the dl of my theme's author template file:
      spiritual_profiles) { ?>
      ALC Certified to Administer the Following Spritual Profiles
      spiritual_profiles; ?>

      • Daniel Quinn Thursday, March 18, 2010 at 11:44 pm

        Because you say it displays the word "array" I suspect it has something to do with your trying to echo an array (echo $array; rather than echo $array[0];. If you upload a plain text file to your server containing only the problematic portion of the script, this may help us.

        • Anne Friday, March 19, 2010 at 12:38 am

          That fixed it! May I suggest that you add that to your tutorial somewhere? Thanks so much!

  48. Dave Saturday, April 10, 2010 at 12:39 pm

    Hi, I have implemented the baove and it works excellently, thanks, however I have a small issue. I need more than one dropdown field. Once added when the users chooses their selected item from each drop down it saves the last option in all of the dropdowns instead of a different option for each.

    I have unique names on all the update_usermeta lines etc.

    Any help would be excellent.

    Thanks

    Dave

    • Daniel Quinn Monday, April 12, 2010 at 11:09 am

      I must advise you to use the newer method for adding custom profile fields to WP:

      http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields

      • Dave Monday, April 12, 2010 at 11:16 am

        Hi, Thanks for the reply. I used the method on the link you mentioned and used the code snippet you supplied for the dropdown, which works perfectly, however im still having trouble getting to save the value of the second dropdown.

        Thanks again.

  49. hamid Thursday, June 17, 2010 at 10:09 am

    Hi!i want add extra field of image upload of user in register Plus plugin in wordpress....plz help me as soon as possible ....

  50. Mike Weinstein Monday, August 16, 2010 at 5:02 pm

    Has anyone been able to use the Members List search form to search for custom meta fields? I have an "organization" field that is definitely getting assigned to my users and showing up on the list, but I cannot search for it.

  51. Niraj Sunday, August 22, 2010 at 11:08 pm

    Hey i hav a question
    is it possible to add a text box and a label in dashboard – in add new post
    so dat the editor gets an easy option to enter the data in proper order and it should be displayed b4 the post after the heading
    for eg in dashboard it should appear in such a way
    Heading-DEMO

    Name-demo
    Place-demo
    Location-demo
    etc
    den post should continue

    is dis possible?
    nd yes den how
    plzzz help me its urgent

Speak Your Mind

For Scathing Rebuttals

Enclose code in <code></code> brackets.

Your Trackbacks

So Says the Blogosphere
  1. WordPress gebruikersprofiel uitbreiden « Ruben Woudsma Friday, November 20, 2009

    [...] omdat hiervoor naar mijn idee geen eenvoudige plugins aanwezig waren.  Op basis van een bericht van Daniel Quinn heb ik een eigen plugin gemaakt met daarin specifieke velden die aan ons doel voldeden. Al met al [...]

  2. Wordpress user registration template and custom user profile fields Monday, May 31, 2010

    [...] http://dquinn.net http://blog.ftwr.co.uk http://rubenwoudsma.nl http://justintadlock.com [...]