Using Custom Profile Fields in WordPress
Creating and working with custom built fields in a WordPress author’s profile can add whole new worlds of functionality to a blog. For example, today I’m going to show you how to create a field in your profile page that will allow authors to submit their own Adsense code so that they can display their own ads on any articles that they write.
It’s as simple as creating the field and then fetching it to use in your theme.
First, we’re going to add an extra text box to the profile page. You can do this by pasting the following code into your theme’s functions.php.
Note: In our example, we’re creating a field where users can submit their personal adsense code. If you want to use this concept for some other purpose then simply replace each instance of “adsense_336″ with the name of your desired field and replace the description with something that accurately describes your custom field.
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) { ?>
<h3>Revenue Sharing: Adsense Information</h3>
<table class="form-table">
<tr>
<th><label for="adsense_336">Adsense 336x280</label></th>
<td>
<textarea type="text" rows="10" cols="60" name="adsense_336" id="adsense_336" value="<?php echo esc_attr( get_the_author_meta( 'adsense_336', $user->ID ) ); ?>" class="regular-text" ></textarea><br />
<span class="description">If you provide your code for a 336x280 adsense ad, we'll use your advertisements on your posts.</span>
</td>
</tr>
</table>
<?php }
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
function my_save_extra_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
/* Copy and paste this line for additional fields. Make sure to change 'twitter' to the field ID. */
update_usermeta( $user_id, 'adsense_336', $_POST['adsense_336'] );
}
Paste the following code into your theme’s single.php as this will allow you to call and display the field that you’ve created.
Note: In our example, I’ve created an area for you to place the house’s ads so that you have something to display when a user doesn’t have personalized ad code applied to their profile.
<?php if(get_the_author_meta('adsense_336')=='') : ?>
<!-- Insert Default Google Adsense Here -->
<!-- End Default Google Adsense Here -->
<?php else: the_author_meta('adsense_336'); endif; ?>
So there you have it. What other ways can you think of that would be useful to use this concept?

Samuel said:
This is a valuable post Nicholas! Thanks for a great idea.
My Latest Blog Post: Finalmente- fuera de Google sites
Nicholas Cardot said:
You’re welcome, Samuel. It’s interesting how many different applications you could come up with built around this concept. The possibilities are really limited only by our imaginations.
Aaron said:
Good info Nicholas. Very cool way to use custom fields in WP. On a side note, I absolutely love your font. Is that Calibri?
My Latest Blog Post: LG 32LD350 32-Inch 720p 60 Hz LCD HDTV Review
Nicholas Cardot said:
Yeah, it sure is Calibri. It might sound strange, but I’m a really big fan of that font. I feel like it’s very simple and easy to read. I like it.
Mandeep said:
Simple and effective, Nicholas. This gives me a couple of ideas I could probably use this for. Thanks for the tutorial.
My Latest Blog Post: The Internet- You and Blogging
Nicholas Cardot said:
You’re welcome. I hope your ideas pan out and you’re able to get them implemented effectively. Good luck.
Dave Doolin said:
Smart.
I encourage guest post authors to use an affiliate link of their own, provided that link is useful to my readers.
IIRC, Den Harsh does something similar at Shout Me Loud, and he’s grown like wildfire in 18 months.
My Latest Blog Post: Scribe SEO- An In-depth review and why your blog needs this tool
Nicholas Cardot said:
I find it’s always a great practice to work to be able to reward those who contribute to your site so I thought this would provide a good example of using a custom profile field.
Devesh said:
Hey Nick,
Great Idea. Very Useful and effective.
Thanks for sharing
.
~Dev
Nicholas Cardot said:
Thanks, Dev. I actually had someone ask me to develop this for one of their clients websites and I thought that it would be excellent as a tutorial.
Ron Leyba said:
Woot! A simple editing that can have a big impact. Short but sweet!
My Latest Blog Post: Free Printable Pumpkin Carving Stencils – JackOLantern Templates
Pacquiao Margarito said:
Well, I’ve been looking for this ‘coz I am maintaining a simple article submission site that I wish to allow users to earn via adsense sharing. I will implement this asap. Thank you very much!
My Latest Blog Post: Pacquiao Now in Baguio City for Intensive Training
Designer's Digest said:
wow, awesome tutorial. I have done this for an extra field for twitter on author’s profile.
My Latest Blog Post: 4 PSD Process Panel In 2 Colors With Hover