O'Reilly

Add and Remove Profile Fields to WordPress User Form

By on  

The WordPress user profile screen allows you to set values for social services but some default services are irrelevant, namely AIM and Yahoo! IM;  add to that the fact that Twitter and Facebook fields are missing.  You quickly realize that the default form...needs work.  WordPress provides a method for adding and removing profile fields.  Let me show you how it works!

Filter Setup

The first step is creating a function in your functions.php file which will accept an array of profile keys and values:

function modify_contact_methods($profile_fields) {

	// Field addition and removal will be done here

}
add_filter('user_contactmethods', 'modify_contact_methods');

This function provides access to that important protected array.  The returned value becomes the list of user profile fields.

Adding a Profile Field

Adding a new field, Twitter handle for example, includes adding a key to the passed in array, with a value which will act as the field label:

function modify_contact_methods($profile_fields) {

	// Add new fields
	$profile_fields['twitter'] = 'Twitter Username';
	$profile_fields['facebook'] = 'Facebook URL';
	$profile_fields['gplus'] = 'Google+ URL';

	return $profile_fields;
}
add_filter('user_contactmethods', 'modify_contact_methods');

Simply adding that key/value to the array adds a new field to the form.

Removing a Profile Field

Conversely, removing a key from said array removes a field from the user profile form:

function modify_contact_methods($profile_fields) {

	// Add new fields
	$profile_fields['twitter'] = 'Twitter Username';
	$profile_fields['facebook'] = 'Facebook URL';
	$profile_fields['gplus'] = 'Google+ URL';

	// Remove old fields
	unset($profile_fields['aim']);

	return $profile_fields;
}
add_filter('user_contactmethods', 'modify_contact_methods');

The code above removes the AIM field from the edit profile form.

Retrieving Custom Field Values

To retrieve custom field values, use the get_the_author_meta method:

// Retrieve a custom field value
$twitterHandle = get_the_author_meta('twitter');

The ability to easily add profile form fields is awesome;  super easy to do, no plugin required!

Track.js Error Reporting

Recent Features

  • 5 Awesome New Mozilla Technologies You’ve Never Heard Of

    My trip to Mozilla Summit 2013 was incredible.  I've spent so much time focusing on my project that I had lost sight of all of the great work Mozillians were putting out.  MozSummit provided the perfect reminder of how brilliant my colleagues are and how much...

  • 5 More HTML5 APIs You Didnât Know Existed

    The HTML5 revolution has provided us some awesome JavaScript and HTML APIs.  Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers.  Regardless of API strength or purpose, anything to help us better do our job is a...

Incredible Demos

  • Google Extension Effect with CSS or jQuery or MooTools JavaScript

    Both of the two great browser vendors, Google and Mozilla, have Extensions pages that utilize simple but classy animation effects to enhance the page. One of the extensions used by Google is a basic margin-top animation to switch between two panes: a graphic pane...

  • Event Delegation with MooTools

    Events play a huge role in JavaScript. I can't name one website I've created in the past two years that hasn't used JavaScript event handling on some level. Ask yourself: how often do I inject elements into the DOM and not add an...

Discussion

  1. If only I saw this a few weeks ago. A great tip. Will be useful to most everybody till the WordPress devs finally take to updating that profile fields.

  2. Adrian

    Simple and helpful, thank you!

    Still, if you’re not a developer comfortable with writing code, Profile Builder (http://wordpress.org/extend/plugins/profile-builder/) may come in handy. Besides adding extra user fields it provides shortcodes for front-end login, register and edit profile forms.

  3. Amit

    Hi,
    I want to create a custom search filter like Date and some custom field for Post.
    can you please help me. how can i manage this on Post Listing Page.

  4. Is there a good way to retrieve all of the added fields in an array and assign them to variables?

  5. I can see immediate use of this, when using the portfolio themes. A CSS designed block and the profile field can be used for the portfolio page. Thanks David, this is giving me a lot of ideas on usage of the fields. :)

  6. tim

    good thing there are others with briliant minds.

    I need to ask you how can i make another page for user profile diferent from the one wordpress gives and how can i add a field for user image.

  7. I am working on a multi-site plugin and this will come handy. Thanks. But is there a simple way to add multiline text field?

  8. THANK YOU! So elegant. Worked perfect.

  9. How about if I want to add fields to a different section other than the contact area? Is this a similar process?

  10. hand-coding

    Hi David, great post.
    How could we store those values in the db as an array instead of separate fields ?

  11. hand-coding

    Just o be clear:
    I’d love to see extra infos displayed as separate fields but stored in a single associative array…

  12. Pat

    Thanks for the post, man! Great stuff!

  13. this was actually informative – not like most of what i see online. sharing :)

  14. I was exactly looking for “Retrieving Custom Field Values”, and finally got it here. Thank you very much.

  15. Thanks man! That helped out a lot!

  16. Liz

    I get a warning message when I hit Update Profile after adding my Google+ url.

    Warning: Cannot modify header information – headers already sent by (output started at themes/selectitaly/functions.php:15) in wp-includes/pluggable.php on line 875.

  17. Surya

    How can I add a custom field in “Add new user” fields

  18. Tieson Wooten

    Thanks for the info, just what I was looking for!

  19. In profile builder I am unable to find how to change profile fields.

  20. thanks for the post.
    I also like the justin tadlock’s method -http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields

  21. Maya

    Awesome, thanks!!!

  22. jos

    Is it possible to rename the existing filename like AIM to ‘ street name’

  23. I does involve writing code but there is a cool plugin http://piklist.com/ that is in beta allows you to use the additional fields anyplace you want in WordPress http://piklist.com/ . Check it out (I just use it. I am not related to the development in any way)

  24. Ulfstein

    Is there an easy way to set this up so that the changes made to function.php get carried over through a WordPress update?

    Many thanks.

  25. i wanted to show the profile picture in the dashbord

  26. Has anyone had trouble getting this code to work in WP 3.8.1? The code has been added to my client’s functions.php file and I’m not seeing any results.

    I’m looking for the additional field on the “Edit User” page, but not seeing it added. Thoughts? Has the hook changed in WP core?

    Thanks for this workaround — I’d love to have it working on our end.

  27. Chris

    Hello, thanks for the post. It works awesome but I’m having a single issue. I get no links for these social fields. I see something like http://www.example.com with no posibility of clicking whatsoever. Am I doing anything wrong ?

  28. Mike Levine

    You have no idea how appreciative I am to have found this post. Seriously, thank you from the bottom of my heart for such a helpful and clear explanation of these items.

  29. Thanks a lot. I didn’t thought it would be so easy!

  30. Lieke

    Thanks, it works great.
    Is there a way to add a checkbox?

  31. Fahad Rafiq

    WordPress websites and blogs are frequent targets of spam comments. To effectively stop the link spammers it is best to remove the “Website” field from the WordPress comment section. Learn 2 methods in this tutorial on how to remove the WordPress website field.
    Source: http://www.cloudways.com/blog/how-to-remove-url-field-from-wordpress-comment-form/

  32. Rub

    Thanks, this help me a lot.

    If I need only to save the data and show it only on the user profile and I do not need to retrieve it, I can only use add option, upload the file and add the data to the user?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!

Recently on David Walsh Blog

  • Loading Static Templates for Intern Testing

    I use Intern by SitePen for all of my JavaScript functional testing.  Intern has loads of features other functional test frameworks don't and it's completely Promise-based -- something I got very used to when I used the Dojo Toolkit every day. Async test creation can...

  • Convert Video to mp3

    Let's all be honest for a moment:  we've all ... not paid for ... music.  Whether it was via a file sharing app like Kazaa or Napster, or it was downloading and seeding on bittorrent, or maybe even downloading a music video and ripping its audio,...

  • Sort git Branches by Date

    I'll be first person to admit I don't do as much git repository maintenance as I should.  I rarely delete branches which have been merged, so a git branch execution shows me a mile-long list of branches that likely aren't relevant.  The best way to find branches I've recently...

  • Best Tools and Resources for Web Professionals in 2015

    Looking for the right resources to help you satisfy the needs of your clients? On the lookout for the best tools to help you increase your revenue? Searching for the right software to help you improve your business? Well, then you’ve come to the right place....

  • JavaScript Polling

    Polling with JavaScript is one of those ugly but important functions within advanced front-end user experience and testing practices.  Sometimes there isn't the event you can hook into to signify that a given task is complete, so you need to get your hands dirty and simply poll for...