WordPress   

Add a custom field in the edit profile page of WordPress dashboard

To add new custom fields in the edit profile page of WordPress dashboard, add the following code snippet in functions.php file of the active WordPress theme.

Note – In this example we will add two custom fields (user_profile_status & tax_code) in the edit profile and show profile page. You can modify the following code snippets according to your requirements to add different custom fields.

//Add custom  field in the edit profile page backend
add_action( 'show_user_profile', 'rp_add_custom_field_edit_profile_wp_backend'); //Add fields on the show profile page
add_action( 'edit_user_profile', 'rp_add_custom_field_edit_profile_wp_backend'); // Add fields on the edit profile page
function rp_add_custom_field_edit_profile_wp_backend( $user )
{ ?>
    <h3><?php _e("User Profile Status", "woocommerce"); ?></h3>
    <table class="form-table">
        <tr>
            <th><label for="user_profile_status"><?php _e("Update Status", "woocommerce"); ?> </label></th>
            <td>
                <input type="radio" class="input-radio " value="1" name="user_profile_status" id="user_profile_status_approved" <?php if( '1' == esc_attr(get_user_meta( $user->ID, 'user_profile_status', true ))){ echo 'checked';} ?>>
                <label for="user_profile_status_approved" class="radio"><?php _e('Approved'); ?></label>
                <input type="radio" class="input-radio " value="0" name="user_profile_status" id="user_profile_status_pending" <?php if( '0' == esc_attr(get_user_meta( $user->ID, 'user_profile_status', true ))){ echo 'checked';} ?>>
                <label for="user_profile_status_pending" class="radio"><?php _e('Pending'); ?></label>
            </td>
         </tr>
         <tr>
            <th><label for="tax_code"><?php _e("Tax Code"); ?> </label></th>
            <td><input type="text" name="tax_code" value="<?php echo esc_attr(get_user_meta( $user->ID, 'tax_code', true )); ?>" class="regular-text" /></td>
         </tr>
    </table>
<?php 
}

Save the values of newly added custom fields

//Update custom field data from edit profile backend
add_action( 'personal_options_update', 'rp_save_custom_field_edit_profile_wp_backend' ); // Update fields from the show profile page
add_action( 'edit_user_profile_update', 'rp_save_custom_field_edit_profile_wp_backend' ); // Update fields from the edit profile page
function rp_save_custom_field_edit_profile_wp_backend( $user_id ) {
    if (isset($_POST['user_profile_status'])) {
       update_user_meta( $user_id, 'user_profile_status', sanitize_text_field( $_POST['user_profile_status'] ) );
    }
    if (isset($_POST['tax_code'])) {
        update_user_meta( $user_id, 'tax_code', sanitize_text_field( $_POST['tax_code'] ) );
    }
}

Add validation to new custom fields

//Validate fields 
add_action('user_profile_update_errors','rp_validate_custom_field_wp_backend_edit_profile',10,1);
function rp_validate_custom_field_wp_backend_edit_profile($args){
    if(isset($_POST['tax_code']) && empty($_POST['tax_code']) ) {
        $args->add( 'error', __( '<strong>Tax Code</strong> is a required field.', 'woocommerce' ),'');
    }
}
Need Professional Support for Your Website Problems?

Whether you're facing website issues or struggling with code implementation, our team is here to assist. Hire us to get your website back on track.

Hire Us