Add Custom Fields to the WordPress User Profile with User Meta

Add custom fields to the WordPress user profile

User data that WordPress does not know, like a phone number, a department or a preference, is stored in user meta, the same key-value storage posts have. To let users and administrators edit it, add fields to the profile screen with two display hooks and save them with two save hooks.

Problem: Users should have a phone number and a department in their profile, editable in Users → Profile, and the values should be usable in templates and queries.

Solution: Print the fields on show_user_profile (own profile) and edit_user_profile (another user’s profile), save them on personal_options_update and edit_user_profile_update with update_user_meta(), and read them with get_user_meta().

The fields

<?php
add_action( 'show_user_profile', 'ha_user_profile_fields' );
add_action( 'edit_user_profile', 'ha_user_profile_fields' );

function ha_user_profile_fields( $user ) {
    ?>
    <h2><?php esc_html_e( 'Contact details', 'textdomain' ); ?></h2>
    <table class="form-table">
        <tr>
            <th><label for="ha_phone"><?php esc_html_e( 'Phone', 'textdomain' ); ?></label></th>
            <td>
                <input type="tel" name="ha_phone" id="ha_phone" class="regular-text"
                       value="<?php echo esc_attr( get_user_meta( $user->ID, 'ha_phone', true ) ); ?>">
            </td>
        </tr>
        <tr>
            <th><label for="ha_department"><?php esc_html_e( 'Department', 'textdomain' ); ?></label></th>
            <td>
                <input type="text" name="ha_department" id="ha_department" class="regular-text"
                       value="<?php echo esc_attr( get_user_meta( $user->ID, 'ha_department', true ) ); ?>">
            </td>
        </tr>
    </table>
    <?php
}

Saving

<?php
add_action( 'personal_options_update', 'ha_save_user_profile_fields' );
add_action( 'edit_user_profile_update', 'ha_save_user_profile_fields' );

function ha_save_user_profile_fields( $user_id ) {
    if ( ! current_user_can( 'edit_user', $user_id ) ) {
        return;
    }

    $fields = array( 'ha_phone', 'ha_department' );

    foreach ( $fields as $key ) {
        if ( isset( $_POST[ $key ] ) ) {
            update_user_meta( $user_id, $key, sanitize_text_field( wp_unslash( $_POST[ $key ] ) ) );
        }
    }
}

The profile form already has its own nonce, which WordPress checks before these hooks run; the capability check makes sure the function also refuses to work when it is called from somewhere else. update_user_meta() creates the field when it does not exist yet, so add_user_meta() is only needed for keys with several values.

Using the values

<?php
// One field of the current user
$phone = get_user_meta( get_current_user_id(), 'ha_phone', true );

// On an author page
$author = get_queried_object();
echo esc_html( get_user_meta( $author->ID, 'ha_department', true ) );

// All editors of one department
$sales = get_users( array(
    'role'       => 'editor',
    'meta_key'   => 'ha_department',
    'meta_value' => 'Sales',
) );

A select field with fixed values (instead of free text) makes queries like the last one reliable: “Sales”, “sales” and “Sales ” are three different values for the database.

NOTE: Use a prefix for your meta keys (ha_phone, not phone). Other plugins (WooCommerce stores billing_phone, for example) and future WordPress versions add their own user meta, and a shared key would mix up the data.