• Feed RSS

Quick Tip: Add Extra Contact Methods to User Profiles

"If you Google “add extra fields to WordPress user profile” you’ll find all sorts of involved coding examples for adding extra inputs to the user profile page so you can capture additional user information. But if all you want to do is expand the default contact methods section then there’s a much simpler way to go.


The user_contactmethods Filter

The user_contactmethods filter allows you to set and unset the contact info fields on the user profile page. The great thing about using this method is that WordPress looks after the creation and updating of the fields.
Let’s add fields for Twitter and Facebook info. Put this in your functions.php file:
add_filter('user_contactmethods', 'my_user_contactmethods');

function my_user_contactmethods($user_contactmethods){

 $user_contactmethods['twitter'] = 'Twitter Username';
 $user_contactmethods['facebook'] = 'Facebook Username';

 return $user_contactmethods;
}
Here is what you’ll get:
If you want to remove some fields, just unset them from the array:
function my_user_contactmethods($user_contactmethods){

 unset($user_contactmethods['yim']);
 unset($user_contactmethods['aim']);
 unset($user_contactmethods['jabber']);

 $user_contactmethods['twitter'] = 'Twitter Username';
 $user_contactmethods['facebook'] = 'Facebook Username';

 return $user_contactmethods;
}
To display the user’s info, simply use the get_user_meta function.
echo get_user_meta(1, 'twitter', true);
This will show the Twitter username for the user with an ID of 1. The true argument causes the data to be returned as a single value as opposed to an array.
That’s all there is to it!"