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);
?>
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 'Phone: ' . esc_html($phone) . '
';
}
if (!empty($company)) {
echo 'Company: ' . esc_html($company) . '
';
}
}
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.