Click here to Skip to main content
12,291,499 members (59,096 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

19.9K views
Posted

How to Add Custom Fields to User Profile in WordPress Using Plugins

, 8 Jan 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
How to Add Custom Fields to User Profile in WordPress Using Plugins

Introduction

WordPress is definitely a very user-friendly and easy platform for creating websites and it provides an easy way to extend/customize the existing core functionality using Plugins. When working with plugins, we often have the need of creating some extra fields associated with the user. WordPress provides hooks with the help of which we can display extra information when the user profile is displayed and perform several actions. This post explains how can we add an extra field to the user profile place and save the same data to DB.

Let us create a plugin and name it “UserFBAddress” that will add an extra field “Facebook Address” to user profile. For this, we will use the following two hooks:

  • show_user_profile
  • personal_options_update

show_user_profile” hook is called when the user profile page is displayed. “personal_options_update” hook is called when the user saves the profile.

Apart from this, we will also use the below two functions:

  • get_user_meta()
  • update_user_meta()

get_user_meta()” function is used to get any data associated with the user with the use of a key. “update_user_meta()” function is used to save any data associated with the user with a key value.

In the plugin file, write the below code and activate the plugin:

add_action('show_user_profile', 'UserFBAddress_add');
add_action('personal_options_update', 'UserFBAddress_update');

function UserFBAddress_add(){ 
    global $user_ID;
    $fb_add = get_user_meta($user_ID, "user_fb_txt");
    if(is_array($fb_add))
        $fbadd = $fb_add[0];
    ?>

Let us understand the code now:

  1. The first thing that we do is we tell WordPress to call our functions when the hooks are being fired. This is done by using add_action().
  2. When the “show_user_profile” hooks is fired, WP will call UserFBAddress_add() and for “personal_options_update”, it will call our “UserFBAddress_update()
  3. Then we define the functions. The UserFBAddress_add() displays the form field to the user.
  4. The UserFBAddress_update() function updates the value of the key for the associated user.

Once the plugin is activated, you will see a new profile field as “Facebook Address” at the bottom of the User Profile page. You can now add as many fields as you want to the User Profile by adding fields in the above functions.

Plugin in action below:

wordpres-plugin

Hope you like this post. Keep learning and sharing!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Nitesh Luharuka
Team Leader Director @ Rebin Infotech
India India
An Independent IT Professional and Consultant on MS Technologies like .Net | MVC | Silverlight | Sharepoint | MS Project Server along with open source CMS Systems like DotNetNuke etc.

I am an enthusiastic technocrat who love to debug problems and solve them. I love writing articles on my website in my spare time. Please visit my Website for more details and subscribe to get technology related tips/tricks.

You may also be interested in...

Comments and Discussions

 
QuestionCode appears incomplete Pin
PaladinDataMatt16-Sep-15 7:13
memberPaladinDataMatt16-Sep-15 7:13 
QuestionNot working for me Pin
ThalaChinna6-Nov-14 19:46
memberThalaChinna6-Nov-14 19:46 
AnswerRe: Not working for me Pin
Nitesh Luharuka6-Nov-14 19:58
memberNitesh Luharuka6-Nov-14 19:58 
GeneralRe: Not working for me Pin
ThalaChinna6-Nov-14 20:06
memberThalaChinna6-Nov-14 20:06 
GeneralRe: Not working for me Pin
Nitesh Luharuka6-Nov-14 20:27
memberNitesh Luharuka6-Nov-14 20:27 
GeneralRe: Not working for me Pin
ThalaChinna6-Nov-14 20:37
memberThalaChinna6-Nov-14 20:37 
GeneralRe: Not working for me Pin
Nitesh Luharuka7-Nov-14 5:53
memberNitesh Luharuka7-Nov-14 5:53 
GeneralMy vote of 3 Pin
EvolMate8-Sep-14 3:52
memberEvolMate8-Sep-14 3:52 
GeneralRe: My vote of 3 Pin
Nitesh Luharuka8-Sep-14 18:06
memberNitesh Luharuka8-Sep-14 18:06 
QuestionHelp implementing into site? Pin
Member 1103447025-Aug-14 2:07
memberMember 1103447025-Aug-14 2:07 
AnswerRe: Help implementing into site? Pin
Member 1103447025-Aug-14 2:24
memberMember 1103447025-Aug-14 2:24 
Questionstuck on profile.php Pin
deemi.exe27-May-14 3:20
memberdeemi.exe27-May-14 3:20 
AnswerRe: stuck on profile.php Pin
Nitesh Luharuka27-May-14 5:23
memberNitesh Luharuka27-May-14 5:23 
GeneralMy vote of 1 Pin
Rich Trefz3-Mar-14 12:25
memberRich Trefz3-Mar-14 12:25 
GeneralRe: My vote of 1 Pin
Nitesh Luharuka4-Mar-14 3:30
memberNitesh Luharuka4-Mar-14 3:30 
GeneralRe: My vote of 1 Pin
Nitesh Luharuka4-Mar-14 4:25
memberNitesh Luharuka4-Mar-14 4:25 
GeneralRe: My vote of 1 Pin
Rich Trefz4-Mar-14 4:41
memberRich Trefz4-Mar-14 4:41 
GeneralRe: My vote of 1 Pin
Nitesh Luharuka4-Mar-14 4:48
memberNitesh Luharuka4-Mar-14 4:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160518.1 | Last Updated 9 Jan 2014
Article Copyright 2014 by Nitesh Luharuka
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid