在用户配置文件中使用和保存自定义下拉框

时间:2013-01-20 作者:Keenan Payne

好的,我正在创建一个网站,需要一些用户配置文件自定义元字段的帮助。目前,我的函数中有以下内容。php文件:

//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\'] );
}
所有的代码似乎都能工作,当我单击“更新配置文件”时,它也能工作(我想),但下拉列表显示的是第一个项目,而不是用户选择的项目。因此,如果用户选择“Yoga Top”并单击“Save”(保存),“I get Guns”(我有枪)将显示,而不是用户选择的“Yoga Top”(瑜伽Top)。有什么我可以解决的吗?

我真的很感激任何帮助,因为我们在时间上已经迫在眉睫了。

谢谢-基南

1 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

您似乎正在检查未设置的变量。

您已设置$selected

$selected = get_the_author_meta( \'user_top\', $user->ID );
但是你检查一下$topselected

<select name="user_top" id="user_top">
    <option value="gotguns" <?php echo ($topselected == "gotguns")?  \'selected="selected"\' : \'\' ?>>I got guns</option>
同样的事情也发生在$middleselected$bottomselected

我认为你的问题只不过是代码中的一个错误。

虽然WordPress有一个函数selected 那会使它更整洁一些。

结束