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.

Read More

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.

Related posts

Leave a Reply

3 comments

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

  3. Add it in your theme’s function.php

    add_action( 'show_user_profile', 'Add_user_fields' );
    add_action( 'edit_user_profile', 'Add_user_fields' );
    function Add_user_fields( $user ) { ?>
        <table class="form-table">
            <tr>
                <th><label for="dropdown">Activate/Deactivate User</label></th>
                <td>
                    <?php 
                    //get dropdown saved value
                    $selected = get_the_author_meta('pmpro_email_confirmation_key', $user->ID); //there was an extra ) here that was not needed 
                    ?>
                    <select name="user_select" id="user_select">
                        <option value="validated" <?php echo ($selected == "validated")?  'selected="selected"' : '' ?>>Activate</option>
                        <option value="notvalidated" <?php echo ($selected == "notvalidated")?  'selected="selected"' : '' ?>>De-Activate</option>
                    </select>
                </td>
            </tr>
        </table>
    <?php } 
    
    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 dropdown
        update_usermeta( $user_id, 'pmpro_email_confirmation_key', $_POST['user_select'] );
        }
    ?>