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.

2
  • how is a dropdown any different then text field?
    – Bainternet
    Apr 24, 2012 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, 2012 at 21:39

3 Answers 3

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'] );
}
1
  • 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?
    – 730wavy
    Sep 14, 2013 at 7:31
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.

1
  • 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?
    – 730wavy
    Sep 14, 2013 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'] );
    }
?>
1
  • This code will helps in having the drop down in user profile page .... Apr 13, 2017 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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