How to add custom fields to users

How to add custom fields to users

Sometimes we need to keep extra information about our users and we can do this by adding new custom fields.

Place this in functions.php
NOTE: if you are using a child theme, place it in child theme’s functions.php

function wcr_user_fields($user) {
    ?>
    <h3><?php _e('Some extra info'); ?></h3>

    <table class="form-table">
        <tr>
            <th><label for="age"><?php _e('Age'); ?></label></th>
            <td>
                <input type="text" name="age" id="age" value="<?php echo esc_attr(get_the_author_meta('age', $user->ID)); ?>" class="regular-text code" /><br />
                <span class="description"><?php _e("Please enter user age."); ?></span>
            </td>
        </tr>
        <tr>
            <th><label for="type"><?php _e('Type'); ?></label></th>
            <td>
                <?php
                $types_array = array(
                    'none',
                    'teacher',
                    'student'
                );
                $checked_value = get_the_author_meta('type', $user->ID);

                foreach ($types_array as $type) {
                    ?>
                    <label for="type-<?php echo $type; ?>"><?php echo ucfirst($type); ?></label>
                    <input type="radio" class="tog" value="<?php echo $type; ?>" id="type-<?php echo $type; ?>" name="type" <?php checked($type, ($checked_value ? $checked_value : 'none')); ?>/><br />
                <?php } ?>
                <span class="description"><?php _e("Please check user type."); ?></span>
            </td>
        </tr>   
        <tr>
            <th><label for="categories"><?php _e('Categories'); ?></label></th>
            <td>
                <?php
                $categories_array = get_categories(array(
                    'orderby' => 'name',
                    'order' => 'ASC'
                ));
                $checked_value = get_the_author_meta('categories', $user->ID);

                foreach ($categories_array as $category) {
                    ?>
                    <label for="category-<?php echo $category->slug; ?>"><?php echo ucfirst($category->name); ?></label>
                    <input type="checkbox" class="tog" value="<?php echo $category->term_id; ?>" id="category-<?php echo $category->slug; ?>" name="categories[]" <?php echo!empty($checked_value) AND in_array($category->term_id, $checked_value) ? 'checked="checked"' : ''; ?>/><br />
                <?php } ?>
                <span class="description"><?php _e("Please check user asigned categories."); ?></span>
            </td>
        </tr>         
    </table>    
    <?php
}

// Add the fields, using our callback function 
add_action('show_user_profile', 'wcr_user_fields');
add_action('edit_user_profile', 'wcr_user_fields');

function wcr_save_user_fields($user_id) {
    // check if current user can edit users
    if (!current_user_can('edit_user', $user_id)) {
        return false;
    } 
	
    // save values
    update_user_meta($user_id, 'age', sanitize_text_field($_POST['age']));
    update_user_meta($user_id, 'type', sanitize_text_field($_POST['type']));
    update_user_meta($user_id, 'categories', array_map('sanitize_text_field', $_POST['categories']));
}

// Save the fields values, using our callback function
add_action('personal_options_update', 'wcr_save_user_fields');
add_action('edit_user_profile_update', 'wcr_save_user_fields');

When you need to show the info just use the get_the_author_meta function:

// get age of the user with ID = 1
echo get_the_author_meta('age', 1);

You can also read about How to add custom fields to categories :)

Leave a Reply

Get your fix of
all cool things WordPress

DigitalOcean hosting coupon 10$ coupon

for DigitalOcean hosting.
Sammy-approved.

Get this deal