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:
- creates a form input, in this case a text input,
- retrieves the value of the option newuserfield from the user meta table in the database
- strips any nasty characters from the information retrieved, and
- 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?
