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!

Recent Features

Incredible Demos

  • By
    HTML5 Context Menus

    One of the hidden gems within the HTML5 spec is context menus. The HTML5 context menu spec allows developers to create custom context menus for given blocks within simple menu and menuitem elements. The menu information lives right within the page so...

  • By
    Create a CSS Cube

    CSS cubes really showcase what CSS has become over the years, evolving from simple color and dimension directives to a language capable of creating deep, creative visuals.  Add animation and you've got something really neat.  Unfortunately each CSS cube tutorial I've read is a bit...

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?

  33. Tel

    How about if you you are adding an image field?

  34. Lennart

    Just a heads up: It seems the newly added field is only being saved for new users. Adding these lines to functions.php and editing existing users doesn’t work.

  35. Hey David,

    Just wanted to say thanks for the awesome post! This solution is 100 times more elegant than most other solutions out there that require manually writing the HTML markup and dealing with POST requests. Thanks again, and keep it up! :-)

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