Using Custom Profile Fields in WordPress

Looking for an amazing yet affordable theme for your site? Join Elegant Themes for only $69, and you're paying less than 50 cents per theme!

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?

Total Shares 0

1 Inbound Link

This awesome site is linking here!

  1. Adding Adsense to Wordpress Through Custom Fields | Internet Genius

Share your Opinion

and be a part of the discussion

  1. *Name (no keywords)
    *Mail
    Website

    * Indicates Required Fields
    Your Comment


13 Lively Comments

We would love to hear yours!


  1. September 28, 2010

    Samuel said:

    This is a valuable post Nicholas! Thanks for a great idea.


    • September 28, 2010

      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.


  2. September 28, 2010

    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?


  3. September 28, 2010

    Mandeep said:

    Simple and effective, Nicholas. This gives me a couple of ideas I could probably use this for. Thanks for the tutorial. :)


  4. September 28, 2010

    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.


    • September 29, 2010

      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.


  5. September 28, 2010

    Devesh said:

    Hey Nick,

    Great Idea. Very Useful and effective.

    Thanks for sharing :).

    ~Dev


  6. September 29, 2010

    Ron Leyba said:

    Woot! A simple editing that can have a big impact. Short but sweet!


  7. October 1, 2010

    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!


  8. October 3, 2010

    Designer's Digest said:

    wow, awesome tutorial. I have done this for an extra field for twitter on author’s profile.




0 Total Shares