User profile custom field

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">
        <tr>
            <th><label>Custom Hash Key</label></th>
            <td><input type="text" value="<?php $_GET['user_phone'];" class="regular-text" /></td>
        </tr>
    </table>
    <?php
}

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

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>
        <td>
            <input name="my_field" type="text" id="my_field" value="<?php echo esc_attr( $fetched_field ); ?>">
        </td>
    </tr><?php
}

// 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 stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x