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!

O'Reilly Velocity Conference
Save 20% with discount code AFF20

Recent Features

  • How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

  • Regular Expressions for the Rest of Us

    Sooner or later you’ll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

Incredible Demos

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

  • Open Files from Command Line on OS X

    I'm as much of a fan of application UIs as anyone else but I'm finding myself working more and more from the command line lately.  Much of that is becoming obsessed with media manipulation but I'm forcing myself to use less UIs so that I...

  • Get Stock Quotes From Command Line

    When I conned my way into my first professional programming gig, I didn't really think much about money -- just that I was getting my foot in the door.  But as my career has gone on, I've been more aware of money, investing, and retirement.  I've recently...

  • Geolocation API

    One interesting aspect of web development is geolocation; where is your user viewing your website from? You can base your language locale on that data or show certain products in your store based on the user's location. Let's examine how you can...

  • Create an Image Preview from a Video

    Visuals are everything when it comes to media.  When I'm trying to decide whether to watch a video on Netflix, it would be awesome to see a trailer of some kind, but alas that isn't available.  When I'm looking to download a video on my computer,...

  • New:  Webdesigner News!

    A new and exciting website has recently been launched for web designers and developers. You likely spend hours every morning browsing through hundreds of posts on your RSS feeds, hoping to stumble across relevant stories. Webdesigner News was built to provide web designers and developers with...