Drop down list in user profile page

I am having problems finding an answer to this. I need to have a custom user meta field.

I can create them which is fine but I need to create a drop down one.

For example I want the user meta to be Unit type and the drop down values to be Residential and Commercial

Any help on this would be great. Thanks.

3 Answers
3

thanks for that. Just one minor tweak, as the first bit of code threw up an error. Below is the working code for the first block of code.

//hooks
add_action( 'show_user_profile', 'Add_user_fields' );
add_action( 'edit_user_profile', 'Add_user_fields' );

function Add_user_fields( $user ) { ?>

<h3>Extra fields</h3>
<table class="form-table">
    <tr>
        <th><label for="text">text</label></th>
        <td>
            <?php 
            // get test saved value
            $saved = esc_attr( get_the_author_meta( 'user_text', $user->ID ) );
            ?>
            <input type="text" name="user_text" id="user_text" value="<?php echo $saved; ?>" class="regular-text" /><br />
            <span class="description">Simple text field</span>
        </td>
    </tr>

    <tr>
        <th><label for="dropdown">dropdown Select field</label></th>
        <td>
            <?php 
            //get dropdown saved value
            $selected = get_the_author_meta( 'user_select', $user->ID ); //there was an extra ) here that was not needed 
            ?>
            <select name="user_select" id="user_select">
                <option value="value1" <?php echo ($selected == "value1")?  'selected="selected"' : '' ?>>Value One label</option>
                <option value="value2" <?php echo ($selected == "value2")?  'selected="selected"' : '' ?>>Value Two label</option>
</select>


            <span class="description">Simple text field</span>
        </td>
    </tr>
</table>

The second part is fine.

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

function save_user_fields( $user_id ) {

if ( !current_user_can( 'edit_user', $user_id ) )
    return false;

//save text field
update_usermeta( $user_id, 'user_text', $_POST['user_text'] );

//save dropdown
update_usermeta( $user_id, 'user_select', $_POST['user_select'] );
}

Leave a Comment