Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
how is a dropdown any different then text field? –  Bainternet Apr 24 '12 at 21:36
    
Because there are two different values that could be set. I need wp to know that there has been a change in the value. –  Fraggy Apr 24 '12 at 21:39
add comment

2 Answers

Once again if you know how to add a text field you know how to add any other form field, ex:

showing the fields:

//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 ) ); 
                ?>
                <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>

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

Saving the fields

//hooks

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'] );
}

in both cases you can see that i use the same function to store the data in the database and to get it from the database no matter what type of field it is.

share|improve this answer
    
hey whenever i try using your code, I get a blank white screen on the profile page. If I take out the dropdowns though, it goes back to normal. Any idea why? –  mygm26 Sep 14 '13 at 7:32
add comment
up vote 0 down vote accepted

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'] );
}
share|improve this answer
    
hey whenever i try using your code, I get a blank white screen on the profile page. If I take out the dropdowns though, it goes back to normal. Any idea why? –  mygm26 Sep 14 '13 at 7:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.