WordPress Membership Sites: Adding and Removing Profile Options Can Be Simple

WordPress Membership Sites: Adding and Removing Profile Options Can Be Simple

When creating sites for my clients, particularly Membership Sites in WordPress, I find that we are often asked to add or remove specific profile fields.

The main fields we are asked to remove are the contact methods of aim, yahoo and Google talk. Not many members use this methods and they clutter the information screen…

Thanks to the wonders of WordPress 2.9 and the ‘unset’ method, all we have to do is add the following function and filter to the functions.php file of our theme.  (Later I’ll write about the wonders that are Filters and Actions in WordPress)

 <?php
function remove_unwanted_contactmethod( $contactmethods ) {
unset($contactmethods['aim']);
unset($contactmethods['jabber']);
unset($contactmethods['yim']);
return $contactmethods;
}
add_filter('user_contactmethods','remove_unwanted_contactmethod',10,1);
?>

I can also confirm that removing the contact fields in this manner, works with the the Theme My Profile plugin, if you want to provide a profile page in the theme layout (not through the wp-admin profile display).

To add a contact method, like Twitter, Facebook or Linked In, is just as easy.  Add a function to your themes function.php file as such:

<?php
function add_new_contactmethods($methods){
$methods['twitter'] = 'Twitter ID';
$methods['facebook'] = 'Facebook';
$methods['linkedin'] = 'Linked In';
return $methods;
}
add_filter('user_contactmethods','add_new_contactmethods');?>

Of course – you’re not just limited to adding these types of contact methods.  You can really add anything you want.

Personal Profile Options

To add ‘personal’ fields to the members profile is nearly as simple.  In the theme functions.php, you can add the following:

 <?php
function my_show_extra_profile_fields( $user ) { ?>
<h3>New User Field</h3>
<table>
<tr>
<th><label for="barcode">New User Field</label></th>
<td><input type="text" name="newuserfield" id="newuserfield" value="<?php echo esc_attr( get_the_author_meta( 'newuserfield', $user->ID ) ); ?>" />
</td>
<td><span>Enter your newuserfield</span></td>
</tr>
</table>
<?php }
function my_save_extra_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
update_usermeta( $user_id, 'newuserfield', $_POST['newuserfield'] );
}
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action('profile_personal_options', 'my_show_extra_profile_fields');
?>

Let’s break this down a little….

The function, my_show_extra_profile_fields, adds the extra field New User Field to the profile page.  In this example, it is a simple text field with the label New User Field.

 <?php
function my_show_extra_profile_fields( $user ) { ?>
<h3>New User Field</h3>
<table>
<tr>
<th><label for="barcode">New User  Field</label></th>
<td><input type="text" name="newuserfield" id="newuserfield"  value="<?php echo esc_attr( get_the_author_meta( 'newuserfield',  $user->ID ) ); ?>" />
</td>
<td><span>Enter your newuserfield</span></td>
</tr>
</table>
<?php }

The ‘meat’ of this function is really this one line:

<input type="text" name="newuserfield" id="newuserfield"   value="<?php echo esc_attr( get_the_author_meta( 'newuserfield',   $user->ID ) ); ?>" />

This line:

  1. creates a form input, in this case a text input,
  2. retrieves the value of the option newuserfield from the user meta table in the database
  3. strips any nasty characters from the information retrieved, and
  4. displays the data retrieved.

The rest of the function sets up the table to display the extra information nicely.

The function, my_save_extra_profile_fields, is called when the member clicks the ‘update profile’ button.  This function looks for anything in the field called newuserfield and writes that information to the user meta table in the database:

<?php
function my_save_extra_profile_fields( $user_id ) {
if (  !current_user_can( 'edit_user', $user_id ) )
return false;
update_usermeta(  $user_id, 'newuserfield', $_POST['newuserfield'] );
}
?>

This works really effectively – and opens the doors to many new ways to connect with your members.  My next challenge is to find a way to display the description box so that HTML and nicely formatted text can be entered….

How are you using your User Profile fields to connect with your members?

  • http://wordpressuccess.org/ Mr WordPres$uccess

    Charly, I enjoyed this very informative post. I haven’t tried the second part on ‘personal’ fields but the first part was precisely what I was looking for. Thank you.

    I just have one question about the code snippets. Some of your single quotes are ‘fancy’. Might this cause problems for people who copy and paste?

  • http://wordpressuccess.org/ Mr WordPres$uccess

    Charly, I enjoyed this very informative post. I haven’t tried the second part on ‘personal’ fields but the first part was precisely what I was looking for. Thank you.

    I just have one question about the code snippets. Some of your single quotes are ‘fancy’. Might this cause problems for people who copy and paste?

  • http://www.berta.net Todor Rangelov

    How can we remove Biographical Info (description)

  • http://www.berta.net Todor Rangelov

    How can we remove Biographical Info (description)

  • http://field2.com Ben Dunkle

    Thanks a lot; I had a problem with the curly quotes but fixed it. How about removing items under the “Personal Options” section, specifically, “Visual Editor”, “Admin Color Scheme”, and “Keyboard Shorcuts”?

  • http://field2.com Ben Dunkle

    Thanks a lot; I had a problem with the curly quotes but fixed it. How about removing items under the “Personal Options” section, specifically, “Visual Editor”, “Admin Color Scheme”, and “Keyboard Shorcuts”?

  • http://www.everparent.com Lee H

    You are sooooo bookmarked! My next great adventure is customizing the profile page for my users and grabbing a bloated plugin to do part of my requirements is out of the question. I almost feel like putting on a pot of coffee and skip sleeping tonight to try it out!

    Sleep is overrated right? :)
    Thanks for taking the time to educate me.
    Lee

  • http://www.everparent.com Lee H

    You are sooooo bookmarked! My next great adventure is customizing the profile page for my users and grabbing a bloated plugin to do part of my requirements is out of the question. I almost feel like putting on a pot of coffee and skip sleeping tonight to try it out!

    Sleep is overrated right? :)
    Thanks for taking the time to educate me.
    Lee

  • Sostegno Pc

    Wow thanks , great post , verry informative. I got 1 problem though , as you can see in this image http://img85.imageshack.us/img85/4493/profileqy.jpg , the New User Field that i created using the function is ON TOP of all other fields. I’d wish it was positioned after the “Linked In” field.
    Any way to do that ?
    Thanks.

  • Anonymous

    I managed to attain my goal by adding more contact methods wth the “function add_new_contactmethods($methods)” but still my question remains valid , how can i move it below all others ? And on top of that how can make the ContactMethods fields REQUIRED , wich means that after the first login , the user must complete them to start adding comments or view products ? Or maybe add them at the register form (i’m using the Theme-my-login plugin)
    Sorry to bother you with these question … but i googled them for 3 days now and still no answer. Thanks again.

  • http://twitter.com/spritchum Kenny Brew

    can this also be used to add fields to a wishlist registration form? Thanks Charley

  • http://askcharlyleetham.com Charly Leetham

    Hi Kenny,

    Unfortunately, no. The WLM registration form (at least at the time of responding, ver 2.50.782) is hardcoded and can’t be modified.

    I use this after a member has registered and get them to update their profile.

    C

  • Larry

    Charly, Thanks for this post. This is really helpful. I saw the link on WLM Insider. I am also interested in a way to delete the rest of the garbage. I really just want a blank slate for a profile page. Seems like the most basic things are the hardest sometimes.

    Anyway Todor, you could just delete the unwanted fields from /wp-admin/user-edit.php, but I don’t know what I am doing enough to know if this is a good idea or not.

  • Larry

    But you’d run into problems when you update.

Powered by WishList Member - Membership Site Software