Get $500+ of the best After Effects files, video templates and music for only $20!
Quick Tip: Add Extra Contact Methods to User Profiles

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!

Add Comment

Discussion 14 Comments

  1. Mifas says:

    Nice Info. Thanks :)

  2. wit says:

    This is insane… I was just going to continue on a project where I needed to do this exact stuff and you post a tutorial about that – and this is not the first time this happens. I dont know how you do it – but just continue with that : D

  3. Don says:

    Great tutorial! I can think of so many applications for this.

    Any thoughts if it’s possible to add a photo upload function for the user as well?

  4. Slobodan says:

    Ha, just did this two days ago for a client. Couldn’t believe how simple it was.

    You can also easily add those fields to WordPress registration page, so users can enter this custom info while registering by doing something like this:

    Twitter
    <input id="twitter" class="input" type="text" tabindex="60" size="25" value="” name=”twitter” />

    Not tested in this form (and I hope I posted it so the code shows correctly) because I butchered it a little bit to only show one field, but it should work.

    Using this, I guess you could integrate with Twitter to send an automated tweet from your site’s Twitter when a new user registers:

    “@TwitterUsername has just registered for SuperWebsite.com…”

    Although you’d probably need to add a checkbox to let users know a tweet will be sent out, and let them disable it. I’m getting carried away :)

  5. Shane says:

    Strange… it didn’t work for me? I added it as the last filter/function in my functions.php file. I don’t know what I’m doing wrong. I copied and pasted it exactly the same as is printed above:

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

  6. Rocky says:

    Hey Ross,

    This really is a great tip, did this just a few days ago, lot of people would find this extremely useful…

    I have a little question. I want to add a “Contact Number” field in the registration page as well as in the user profile page. Could anyone give a suggestion as to how to do it. I have already used Cimy Extra User Field plugin, looking for something else though!!

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.