Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / operating-systems / Windows

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

4.67/5 (3 votes)
8 Jan 2014CPOL2 min read 34.8K  
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:

PHP
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];
    ?>
    <h3>Extra Fields</h3>
    <table class="form-table">
        <tr>
            <th><label for="user_fb_txt">Facebook Address</label></th>
            <td><input type="text" id="user_fb_txt" 
            name="user_fb_txt" value="<?php echo $fbadd; ?/>" /><br />
            <span class="description">Enter your Facebook Address here.</span></td>
        </tr>
    </table>
    < ?php            
        }

function UserFBAddress_update(){
    global $user_ID;
    update_user_meta($user_ID, "user_fb_txt",$_POST['user_fb_txt']);    
}

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)