User profile custom field

The question:

Good day, I’ve added the following code to my theme’s functions.php file, which should display a custom field on the user profile screens:

add_action( 'show_user_profile', 'display_user_custom_hash' );
add_action( 'edit_user_profile', 'display_user_custom_hash' );
function display_user_custom_hash( $user ) { ?>
    <h3>USERMETA Fields</h3>
    <table class="form-table">
            <th><label>Custom Hash Key</label></th>
            <td><input type="text" value="<?php $_GET['user_phone'];" class="regular-text" /></td>

After updating the user profile, the value is not visible in the text input. How can I show the value in the text input after updating the user’s profile?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Here is an example using a field called my_field. The value is successfully saved and displayed:

// Declaring the form fields
add_action( 'show_user_profile', 'wpse_show_my_fields' );
add_action( 'edit_user_profile', 'wpse_show_my_fields' );
add_action( 'user_new_form',     'wpse_show_my_fields' );
function wpse_show_my_fields( $user ) {
    $fetched_field = get_user_meta( $user->ID, 'my_field', true ); ?>
    <tr class="form-field">
        <th scope="row"><label for="my_field"><?php _e( 'Field Name', 'text-domain' ) ?> </label></th>
            <input name="my_field" type="text" id="my_field" value="<?php echo esc_attr( $fetched_field ); ?>">

// Saving my form fields
add_action( 'personal_options_update',  'wpse_save_my_form_fields' );
add_action( 'edit_user_profile_update', 'wpse_save_my_form_fields' );
add_action( 'user_register',            'wpse_save_my_form_fields' );
function wpse_save_my_form_fields( $user_id ) {
    update_user_meta( $user_id, 'my_field', $_POST['my_field'] );

add_action( 'edit_user_created_user', 'wpse_edit_user_created_user', 10, 2 ); // for user-new.php page new user addition
function wpse_edit_user_created_user( $user_id, $notify ) {
    $meta = get_user_meta( $user_id, 'my_field', true );

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment