2

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.

  • 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
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'] );
}
| 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? – Rich Sep 14 '13 at 7:31
1

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.

| 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? – Rich Sep 14 '13 at 7:32
0

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'] );
    }
?>
| improve this answer | |
  • This code will helps in having the drop down in user profile page .... – narinder kumar Apr 13 '17 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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