How to create a new field in the WordPress user section?

Example: Creating a new field named Skills

Step 1: Write the below-given code in function.php in the current theme.

function wk_custom_user_profile_skills_fields( $user ) {
    ?>
    <h3 class="heading">Skills</h3>
    <table class="form-table">
        <tr>
            <th><label for="department">Skills</label></th>
			  <td>
                <textarea name="skills" id="skills" class="regular-text"><?php echo esc_textarea( get_user_meta( $user->ID, 'skills', true ) ); ?></textarea>
            </td>
        </tr>
    </table>
    <?php
}

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

/**
 * Save custom user profile fields.
 *
 * @param int $user_id User ID.
 */
function wk_save_custom_user_profile_skills_fields( $user_id ) {
    if ( current_user_can( 'edit_user', $user_id ) ) {
        update_user_meta( $user_id, 'skills', sanitize_text_field( $_POST['skills'] ) );
    }
}

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

Simply by adding the above code we can create a new field in the WordPress user section:

Screenshot:

The same procedure is used for creating fields with another name in WordPress.

Leave a comment

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