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.

Okay, so I'm creating a website and am in need of help for some custom meta fields for user profiles. Currently I have the following in my functions.php file:

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

function Add_user_fields( $user ) { ?>

<h3 class="sizeShapeH3">Size &amp; Shape</h3>
<table class="form-table">
    <tr>
        <th><label for="dropdown">Top (neck, shoulders, arms) </label></th>
        <td>
            <?php 
            //get dropdown saved value
            $selected = get_the_author_meta( 'user_top', $user->ID ); //there was an extra ) here that was not needed 
            ?>
            <select name="user_top" id="user_top">
                <option value="gotguns" <?php echo ($topselected == "gotguns")?  'selected="selected"' : '' ?>>I got guns</option>
                <option value="yogatop" <?php echo ($topselected == "yogatop")?  'selected="selected"' : '' ?>>Yoga top</option>
                <option value="waifish" <?php echo ($topselected == "waifish")?  'selected="selected"' : '' ?>>Waif-ish</option>
            </select>
        </td>
    </tr>
    <tr>
        <th><label for="dropdown">Middle (bust, waist) </label></th>
        <td>
            <?php 
            //get dropdown saved value
            $selected = get_the_author_meta( 'user_middle', $user->ID ); //there was an extra ) here that was not needed 
            ?>
            <select name="user_middle" id="user_middle">
                <option value="handful" <?php echo ($middleselected == "handful")?  'selected="selected"' : '' ?>>Perfect handful</option>
                <option value="yogawaist" <?php echo ($middleselected == "yogawaist")?  'selected="selected"' : '' ?>>Yoga Waist</option>
                <option value="itsybitsy" <?php echo ($middleselected == "itsybitsy")?  'selected="selected"' : '' ?>>Itsy Bitsy</option>
            </select>
        </td>
    </tr>
    <tr>
        <th><label for="dropdown">Bottom (hips/thighs) </label></th>
        <td>
            <?php 
            //get dropdown saved value
            $selected = get_the_author_meta( 'user_bottom', $user->ID ); //there was an extra ) here that was not needed 
            ?>
            <select name="user_bottom" id="user_bottom">
                <option value="junktrunk" <?php echo ($bottomselected == "junktrunk")?  'bottomselected="bottomselected"' : '' ?>>Junk in the trunk</option>
                <option value="yogabottom" <?php echo ($bottomselected == "yogabottom")?  'bottomselected="bottomselected"' : '' ?>>Yoga Bottom</option>
                <option value="shrunktrunk" <?php echo ($bottomselected == "shrunktrunk")?  'bottomselected="bottomselected"' : '' ?>>Shrunk in the trunk</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 top
    update_usermeta( $user_id, 'user_top', $_POST['user_top'] );

    //save middle
    update_usermeta( $user_id, 'user_middle', $_POST['user_middle'] );

    //save bottom
    update_usermeta( $user_id, 'user_bottom', $_POST['user_middle'] );
}

All of the code seems to work and when I click "Update Profile" it works (I think), but the dropdowns display the first item, instead of the selected item by the user. So if a user selected "Yoga Top" and clicked on "Save", "I Got Guns" is displayed instead of "Yoga Top", which the user selected. Is there anyway I can fix this?

I would really appreciate any help as we are under the gun in terms of time.

Thanks! -Keenan

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

You appear to be checking for variables that aren't set.

You set $selected

$selected = get_the_author_meta( 'user_top', $user->ID );

But then you check for something called $topselected

<select name="user_top" id="user_top">
    <option value="gotguns" <?php echo ($topselected == "gotguns")?  'selected="selected"' : '' ?>>I got guns</option>

The same thing happns with $middleselected and $bottomselected

I think your problem is nothing more than a mistake in the code.

Though WordPress has a function called selected that would make that a little neater.

share|improve this answer
    
Perfect, thank you so much. It was pretty late at night so that was a simple mistake. –  Keenan Payne Jan 20 '13 at 17:00
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.