Recently, I had the need to have custom fields in my WordPress user profiles. The specific reason I needed it was to be able to show different content to different users based on certain attributes. Also, I needed to echo out some values to those users as instructions or directions.
So, as I typically do, I went searching for a plugin that performs this task. This is my first step in the process because I’m a big fan of “not reinventing the wheel” and if someone out there has done this already I should be able to implement it and continue to work on my specific features.
I found a lot of plugins that do this task. However, they all had a basic foundation in user registration and the users themselves editing/updating/viewing their profiles. Some of the values I needed to give to users, I didn’t want them to edit. Finally I settled on one by ThemeFuse called “Extend User Profile“. (NOTE: This plugin hasn’t been updated since 2012 and I don’t really recommend it – keep reading)
I settled on this plugin because time was short and I still needed to do a lot of coding. Ultimately, it was a pain in the ass though because while it did allow me to add custom user fields to the profiles, it stored them all in a single meta value in the WordPress database. I couldn’t get rid of the default ones that came with it (Facebook, LinkedIn, & Twitter) which I didn’t need either. Managing the users was a pain and it took 3 of us at work to figure out how to import the almost 1,800 users I needed to import. The code I had to write to grab those values was a bit long as well.
Once I got it to work and had all the users in there, it performed the way I needed it to. This was a short lived site anyway.
Phase 2
Then Phase 2 came along. I already knew I was going to dump this plugin and I went on a search for a different one. Clearly someone must have accomplished this before, right? After searching many different corners of the Internet, I decided everything I found was either too complicated and I’d have to trick it to get it to work the way I needed (bad idea – see above) or it was overkill for what I needed. That leaves one remaining solution: write my own plugin.
I’m no stranger to writing my own plugins, so this wasn’t a huge deal to me. It just meant I’d have to spend more time coding than I had originally planned. So I went out to figure out how to code in more fields into the user profiles. The answer I found really surprised me: This is so flippin easy to do.
To be honest, it really shouldn’t have surprised me that it’s easy to code your own custom fields. After all, this is WordPress and the user profile has been around forever.
So if you need a simple solution to programming custom fields into your user profiles, see the code below.
This is the function that will show the input fields in the user profile page of the WordPress Admin. (goes in functions.php)
- I always put
im_ in front of any function name I create.
- Replace
FirstCustomField with whatever you want to name the field.
|
function im_custom_profile_fields( $user ) { ?> <h3>Custom Fields</h3> <table class="form-table"> <tr> <th><label for="FirstCustomField">First Custom Field</label></th> <td> <input type="text" name="FirstCustomField" id="FirstCustomField" value="<?php echo esc_attr( get_the_author_meta( 'FirstCustomField', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description">This is the description of what that field is.</span> </td> </tr> </table> <?php } |
If you need more fields, just copy/paste from the
<tr> to the
</tr> and replace the labels for your new field. Now, just below this function you’ll need to add these two lines of code that will run the function above. (goes in functions.php)
|
add_action( 'show_user_profile', 'im_custom_profile_fields' ); add_action( 'edit_user_profile', 'im_custom_profile_fields' ); |
But we’re not done quite yet. Now we need to tell WordPress to save the data into the database. (goes in functions.php)
|
function im_save_custom_profile_fields( $user_id ) { if ( !current_user_can( 'edit_user', $user_id ) ) return false; /* For each field added, copy/paste the line below and update the value to the variable name you gave */ update_usermeta( $user_id, 'FirstCustomField', $_POST['FirstCustomField'] ); } add_action( 'personal_options_update', 'im_save_custom_profile_fields' ); add_action( 'edit_user_profile_update', 'im_save_custom_profile_fields' ); |
Upload your new functions.php file and you should now see your new field[s] in the user profile section.
Extending
If you need to use these fields in other places of your site (like I did), there’s an easy way to grab them.
To echo the value out to the browser window, simply call
the_author_meta( $meta_key, $user_id ) in your theme file or custom function where you want that value to be displayed. Note that
$meta_key is the name of the field you gave. So if I wanted to display the value from the above example I would add:
|
<?php the_author_meta( 'FirstCustomField', $user_id ); ?> |
In my theme file where I wanted that to be displayed.
If you want to grab that value as a variable instead the code you use is
get_the_author_meta( $meta_key, $user_id ) . So let’s say (again, using the above example) I wanted to store the value of
FirstCustomField into a variable named
$myfield for use later. I would use this code below:
|
<?php $myfield = the_author_meta( 'FirstCustomField', $user_id ); ?> |
Hopefully this will save someone some time browsing around for plugins if you’re just looking for a simple way to add a few custom fields.
The next part of what I’ll be doing is adding an import function that will take a bunch of users from something like a CSV and import the users along with their custom fields and values.