Snippet: Add Extra User Profile Fields

This is the next step to our previous snippet “Add Custom User Profile Fields” where user can only add text fields to user’s profile. Now, we will proceed further and tell you how you can add any type of field in your profile. We are going to add country (a select field), gender (a radio field) and cell number (a text field).

Usage

get_user_meta($user_id, 'user_country', true);
get_user_meta($user_id, 'user_gender', true);
get_user_meta($user_id, 'user_cell', true);

Snippet

function custom_user_profile_fields($user) {
    global $neu_utility;
    $country = esc_attr(get_the_author_meta('user_country', $user->ID));
    $cell = esc_attr(get_the_author_meta('user_cell', $user->ID));
    $gender= esc_attr(get_the_author_meta('user_gender', $user->ID));
    
    ?>
    <h3><?php _e('Extra User Fields', 'your_domain'); ?></h3>
    <table class="form-table">
        <tr>
            <th>
                <label for="user_country"><?php _e('Country', 'your_domain'); ?>
                </label></th>
            <td>
                <select name="user_country" id="user_country" >
                    <option value="United States" <?=($country=='United States') ? 'selected="selected"':''?> >United States</option>
                    <option value="UK" <?=($country=='UK') ? 'selected="selected"':''?> >United Kingdom</option>
                    <option value="Canada" <?=($country=='Canada') ? 'selected="selected"':''?> >Canada</option>
                    <option value="Australia" <?=($country=='Australia') ? 'selected="selected"':''?> >Australia</option>
                    <option value="Germany" <?=($country=='Germany') ? 'selected="selected"':''?> >Germany</option>
                </select>
                <br />
            </td>
        </tr>
        <tr>
            <th>
                <label for="user_gender"><?php _e('Gender', 'your_domain'); ?>
                </label></th>
            <td>
                <input type="radio" name="user_gender" value="Male" <?=($gender=='Male') ? 'checked="checked"':''?> >Male<br/>
                <input type="radio" name="user_gender" value="Female" <?=($gender=='Female') ? 'checked="checked"':''?> >Female
                <br />
            </td>
        </tr>
        <tr>
            <th>
                <label for="user_cell"><?php _e('Cell Number', 'your_domain'); ?>
                </label></th>
            <td>
                <input type="text" name="user_cell" id="user_city" value="<?=$cell ?>" class="regular-text">
                <br />
            </td>
        </tr>
    </table>
<?php
}

function save_custom_user_profile_fields($user_id) {
    if (!current_user_can('edit_user', $user_id))
        return FALSE;
    update_usermeta($user_id, 'user_country', $_POST['user_country']);
    update_usermeta($user_id, 'user_gender', $_POST['user_gender']);
    update_usermeta($user_id, 'user_cell', $_POST['user_cell']);
}

add_action('show_user_profile', 'custom_user_profile_fields');
add_action('edit_user_profile', 'custom_user_profile_fields');
add_action('personal_options_update', 'save_custom_user_profile_fields');
add_action('edit_user_profile_update', 'save_custom_user_profile_fields');

2 Responses to Snippet: Add Extra User Profile Fields

  1. You should really parse those cell numbers before you add them to the database… the following would format for US numbers only. The result would be 000-000-0000, regardless of what the user enters ex. – (000) 000-0000, 000.000.0000, (000)-000-0000 would all result in 000-000-0000 being entered into the database.

    I would add this before “update_usermeta”, and change the update call for “$_POST['user_cell']

    $user_clean_cell = preg_replace('/\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})/', '\1-\2-\3', $_POST['user_cell']);
    update_usermeta($user_id, 'user_cell', $user_clean_cell);

    • Mohsin Rasool says:

      Thanks Jon, It would be helpful for those who want to save country specific formatted phone number. Thanks for your contribution.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>