User Contact Fields in WordPress

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!

Tags: , , ,


Yoast.com runs on the Genesis Framework

Genesis theme frameworkThe Genesis Framework empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides you with the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go.

Read our Genesis review or get Genesis now!

20 Responses

  1. Ryan HellyerBy Ryan Hellyer on 14 June, 2012

    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.

    • Joost de ValkBy Joost de Valk on 14 June, 2012

      Agreed, they’re useless now…

  2. Gautam DoddamaniBy Gautam Doddamani on 14 June, 2012

    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! :)

    • Kenneth C YoungBy Kenneth C Young on 24 June, 2012

      Would you mind explaining how and where to put the rel=author in my wordpress application?

      Kenneth

  3. Robin JenningsBy Robin Jennings on 15 June, 2012

    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.

  4. Albert PaulBy Albert Paul on 18 June, 2012

    Informative post,You’ve always come up with the best one.I really needed to add twitter to my site.

  5. RobertBy Robert on 20 June, 2012

    Hi,

    How can I remove the google+ (googleplus) from the user meta page?
    My users are companies the don’t use this.

    Robert

    • Joost de ValkBy Joost de Valk on 20 June, 2012

      You can unset it, read the previous post here.

      • RobertBy Robert on 20 June, 2012

        Sorry Joost do you mean the unset( $contactmethods['googleplus'] ); method?

        Robert

        • Joost de ValkBy Joost de Valk on 20 June, 2012

          Yes. Make sure the filter runs after mine.

  6. RobertBy Robert on 20 June, 2012

    I put this in my functions.php how do I know that this runs after your code?

  7. Dolphin247By Dolphin247 on 21 June, 2012

    Hi,
    Your article is really interesting and very nice, i will come often to read your article.

  8. arjun aswalBy arjun aswal on 21 June, 2012

    Nice work brother. carry on..

  9. Dolphin247By Dolphin247 on 22 June, 2012

    Really great article keep writing

  10. Haroun KolaBy Haroun Kola on 23 June, 2012

    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?

  11. TomBy Tom on 2 July, 2012

    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

    • TomBy Tom on 2 July, 2012

      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 )));

  12. Marc NashaatBy Marc Nashaat on 10 July, 2012

    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.