WordPress comes with a “default” set of user contact fields, which has always looked random to me: AIM, Yahoo IM and Jabber / Google Talk, instead of what I’d want to have there: Twitter, Facebook and Google+. A while back I got frustrated enough to have a look at how this was actually dealt with in the backend of WordPress, and fix it, so in WordPress 2.9 an API was added as a result of ticket #10240.
This change allowed for developers to add user contact fields in a very simple way. The example code below adds Twitter and removes Yahoo IM, and yes, this is all the code that’s needed to do it:
function add_twitter_contactmethod( $contactmethods ) {
// Add Twitter
if ( !isset( $contactmethods['twitter'] ) )
$contactmethods['twitter'] = 'Twitter';
// Remove Yahoo IM
if ( isset( $contactmethods['yim'] ) )
unset( $contactmethods['yim'] );
return $contactmethods;
}
add_filter( 'user_contactmethods', 'add_twitter_contactmethod', 10, 1 );
Easy does it, right? Retrieving the user contact field isn’t much harder either, for example, if you’re on a singular page, use the following code:
if ( is_singular() ) {
global $post;
$twitter = get_the_author_meta( 'twitter', $post->post_author );
}
And there you have it! My WordPress SEO plugin uses this exact method to add a Google+ profile field for users to be used in conjunction with the author highlighting features of Google. If you’d want to echo the Google+ user contact field that plugin creates, do the following:
if ( is_singular() ) {
global $post;
the_author_meta( 'googleplus', $post->post_author );
}
As you can see this uses the_author_meta instead of get_the_author_meta. The $post global and the $post->post_author reference would strictly speaking not even be needed as they’re called from the global otherwise within the function, but this might prevent issues with retrieving the wrong user contact fields.
That’s it! Now you can add and remove user contact fields as you wish. You can do this in your theme’s functions.php or in a plugin, be sure to add the isset code around it to prevent notices though!



Thanks. They should probably remove those default links as they’re kinda pointless in their current guise and may change again in the future depending on which networks are most popular.
Agreed, they’re useless now…
another great tut..thanks joost…i have enabled the rel=author function in the wordpress seo…was surprised to see the new google+ field in my profile page..now i know how to do it! :)
Would you mind explaining how and where to put the rel=author in my wordpress application?
Kenneth
I had a meeting last week with a new client who asked exactly what is Yahoo IM and Jabber Talk? And why can’t he have a link for Facebook. Very timely indeed.
Informative post,You’ve always come up with the best one.I really needed to add twitter to my site.
Hi,
How can I remove the google+ (googleplus) from the user meta page?
My users are companies the don’t use this.
Robert
You can unset it, read the previous post here.
Sorry Joost do you mean the
unset( $contactmethods['googleplus'] );method?Robert
Yes. Make sure the filter runs after mine.
I put this in my functions.php how do I know that this runs after your code?
Run it with a lower priority (a higher number) than mine, mine is the default 10, so setting it 20, for instance, would work. See the Codex about add_filter: http://codex.wordpress.org/Function_Reference/add_filter
Thanks works with 20, learned something again ;)
Robert
Hi,
Your article is really interesting and very nice, i will come often to read your article.
Nice work brother. carry on..
Really great article keep writing
Thanks Joost, great tutorial. I know how the practical reason for using Google+ profile, it gives more prominence in SERPs if your G+ pic is there, but what practical use is the Twitter contact field and how can one use it?
Hi Joost,
I’m not too familiar with PHP, so could you please explain how we would go about removing all 3 default fields (Yim, Aim, Jabber), and replacing with three new fields (Twitter, Facebook, and Twitter)?
This was my attempt, but it doesn’t work:
function add_twitter_contactmethod( $contactmethods ) {
// Add Twitter
if ( !isset( $contactmethods['twitter'] ) )
$contactmethods['twitter'] = 'Twitter';
if ( !isset( $contactmethods['facebook'] ) )
$contactmethods['twitter'] = 'Facebook';
if ( !isset( $contactmethods['googleplus'] ) )
$contactmethods['twitter'] = 'Google Plus';
// Remove Yahoo IM
if ( isset( $contactmethods['yim'] ) )
unset( $contactmethods['yim'] );
// Remove AIM
if ( isset( $contactmethods['aim'] ) )
unset( $contactmethods['aim'] );
// Remove Jabber/ Google Talk
if ( isset( $contactmethods['jabber'] ) )
unset( $contactmethods['Jabber'] );
return $contactmethods;
}
add_filter( 'user_contactmethods', 'add_twitter_contactmethod', 'add_googleplus_contactmethod', 'add_googleplus_contactmethod', 10, 1 );
Thanks
Actually (despite not knowing PHP), I managed to figure this out for myself… who’s a clever lad ;)
Here it is, for anyone else wondering how to do it:
function add_twitter_contactmethod( $contactmethods ) {
// Add Twitter
if ( !isset( $contactmethods['twitter'] ) )
$contactmethods['twitter'] = 'Twitter';
if ( !isset( $contactmethods['facebook'] ) )
$contactmethods['facebook'] = 'Facebook';
if ( !isset( $contactmethods['googleplus'] ) )
$contactmethods['googleplus'] = 'Google Plus';
// Remove Yahoo IM
if ( isset( $contactmethods['yim'] ) )
unset( $contactmethods['yim'] );
// Remove AIM
if ( isset( $contactmethods['aim'] ) )
unset( $contactmethods['aim'] );
// Remove Jabber/ Google Talk
if ( isset( $contactmethods['jabber'] ) )
unset( $contactmethods['jabber'] );
return $contactmethods;
}
add_filter( 'user_contactmethods', 'add_twitter_contactmethod', __('add_facebook_contactmethod', __('add_googleplus_contactmethod', 10, 1 )));
Awesome…I never got that whole AIM thing its 2012 who the hell still uses AIM. Anyways, This is my first comment..decided to come read your blog after discovering that without doing a single thing to optimize my site other then using the yoast seo plugin AND while my site was still in under construction (with literally one piece of content) I began ranking 3rd position for the term “wtf is seo”. Granted its not a highly competitive phrase still with about 4000 results in the SERPs I was surprised to see that happen over night.