WordPress: Add or remove contact methods to user profiles
The contact methods available in WordPress user profile page needs a major overhaul. Yahoo, AIM and Jabber are no longer the preferred social messaging services. You can remove some contact methods from WordPress user profile data and include more popular messaging services and social media profiles.
Using the “user_contactmethods” filter, you can add (set) and remove (unset) the contact information fields on the user profile page. You just need to add a few lines of code in the functions.php file of the theme.
Here is how you can remove the unused contact methods from the profile page and add Facebook and Twitter.
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 retrieve the new contact methods/social media profile links of users, you can use “get_user_meta” function.
Use the code to display the Twitter handle of WordPress user with ID as 1.
<?php echo get_user_meta(1, 'twitter', true); ?>
If you want to display Facebook username of the author of a blog post, use the following code in the theme’s single.php file.
<?php the_author_meta('facebook'); ?>