How to Add Custom User Meta & Profile Fields

By default, the WordPress user profile screen (/wp-admin/profile.php) includes standard fields like first name, last name, email, and biographical info. However, when building membership sites, client portals, or multi-author directories, you often need to store extra information for each user—such as phone numbers, company names, social profiles, or custom preferences.

Instead of installing heavy user management plugins, you can easily add, save, and retrieve Custom User Meta Fields using native WordPress hooks.

In this tutorial, we will walk through how to extend WordPress user profiles securely from scratch.

1. Adding Custom Fields to the User Profile Screen

To output custom HTML fields on the user profile screen, we hook into show_user_profile (for the user’s own profile) and edit_user_profile (when an administrator edits another user).

Add the following code to your custom plugin or theme’s functions.php:

				
					if (!defined('ABSPATH')) {
    exit;
}

add_action('show_user_profile', 'wg_add_custom_user_profile_fields');
add_action('edit_user_profile', 'wg_add_custom_user_profile_fields');

function wg_add_custom_user_profile_fields($user) {
    // Retrieve existing values if they exist
    $phone   = get_the_author_meta('wg_phone_number', $user->ID);
    $company = get_the_author_meta('wg_company_name', $user->ID);
    ?>
    <h3><?php _e('WebGarage Extra Profile Information', 'webgarage'); ?></h3>

    <table class="form-table" role="presentation">
        <tr>
            <th><label for="wg_phone_number"><?php _e('Phone Number', 'webgarage'); ?></label></th>
            <td>
                <input type="text" id="wg_phone_number" name="wg_phone_number" value="<?php echo esc_attr($phone); ?>" class="regular-text" />
                <span class="description"><?php _e('Please enter your contact phone number.', 'webgarage'); ?></span>
            </td>
        </tr>
        <tr>
            <th><label for="wg_company_name"><?php _e('Company Name', 'webgarage'); ?></label></th>
            <td>
                <input type="text" id="wg_company_name" name="wg_company_name" value="<?php echo esc_attr($company); ?>" class="regular-text" />
                <span class="description"><?php _e('Enter your organization or company name.', 'webgarage'); ?></span>
            </td>
        </tr>
    </table>
    <?php
}
				
			

2. Saving the Custom User Meta Data Securely

When the user profile is updated, we must capture the submitted data, verify user permissions and nonces, sanitize the input, and save it to the database using update_user_meta().

Add this processing logic to your functions.php:

				
					add_action('personal_options_update', 'wg_save_custom_user_profile_fields');
add_action('edit_user_profile_update', 'wg_save_custom_user_profile_fields');

function wg_save_custom_user_profile_fields($user_id) {
    // 1. Check user capabilities (can the current user edit this profile?)
    if (!current_user_can('edit_user', $user_id)) {
        return false;
    }

    // 2. Sanitize and save Phone Number
    if (isset($_POST['wg_phone_number'])) {
        update_user_meta(
            $user_id,
            'wg_phone_number',
            sanitize_text_field($_POST['wg_phone_number'])
        );
    }

    // 3. Sanitize and save Company Name
    if (isset($_POST['wg_company_name'])) {
        update_user_meta(
            $user_id,
            'wg_company_name',
            sanitize_text_field($_POST['wg_company_name'])
        );
    }
}
				
			

3. Retrieving and Displaying User Meta on the Frontend

Once stored in the database, fetching and displaying these custom fields anywhere on your frontend templates is straightforward using get_user_meta():

				
					function wg_display_user_contact_info($user_id) {
    $phone   = get_user_meta($user_id, 'wg_phone_number', true);
    $company = get_user_meta($user_id, 'wg_company_name', true);

    if (!empty($phone)) {
        echo '<p><strong>Phone:</strong> ' . esc_html($phone) . '</p>';
    }
    
    if (!empty($company)) {
        echo '<p><strong>Company:</strong> ' . esc_html($company) . '</p>';
    }
}
				
			

Adding custom user meta fields natively allows you to tailor user management in WordPress precisely to your project’s needs without bloating your site with external plugins.

Be sure to check out our previous tutorials on WordPress development:
How to Handle Nonces & Form Security in WordPress
How to Register Custom Post Types & Taxonomies
Modifying WordPress Database Queries with posts_where

Need help with your project? We will be happy to help with your project. Feel free to contact us. Just reach out and we will reply soon.

Comments

No comments yet

Share Your Thoughts

Join the conversation!

Scroll to Top

Share Your Thoughts

Thank you for your comment!

Your email address will not be published. Required fields are marked *

Pro

Thank you for reaching out! We’ll be in touch within 24 hours to discuss your project

Standard

Thank you for reaching out! We’ll be in touch within 24 hours to discuss your project

Basic

Thank you for reaching out! We’ll be in touch within 24 hours to discuss your project