如何从用户配置文件的下拉列表中保存多个选项

时间:2015-04-04 作者:Ami

我使用此代码创建下拉列表,然后保存下拉列表选择的值:

<?php
// Display Fields 
add_action( \'show_user_profile\', \'add_multiple_choice_dropdown \' );
add_action( \'edit_user_profile\', \'add_multiple_choice_dropdown \' );

function add_multiple_choice_dropdown ( $user ) {
?>

<h3>Extra profile information</h3>

<table class="form-table"> 
<tr>
    <th><label for="multi_dropdown">The dropdown with multiple choices</label></th>
    <td>
        <?php 
        //get dropdown saved value
        $selected = esc_attr(get_user_meta( $user->ID, \'multi_dropdown\', true )); 
        ?>
    <select name="multi_dropdown" id="multi_dropdown" multiple>
            <option value="first_choice" <?php echo ($selected == "first_choice")?  \'selected="selected"\' : \'\' ?>>First Choice</option>
            <option value="second_choice" <?php echo ($selected == "second_choice")?  \'selected="selected"\' : \'\' ?>>Second Choice</option>
            <option value="third_choice" <?php echo ($selected == "third_choice")?  \'selected="selected"\' : \'\' ?>>Third Choice</option>
    </select>
    <p class="description">Choose from the options above.</p>
    </td>
</tr>
</table>

<?php
}

// Save fields
add_action( \'personal_options_update\', \'save_multiple_choices\' );
add_action( \'edit_user_profile_update\', \'save_multiple_choices\' );

function save_multiple_choices( $user_id )
{
if ( isset( $_POST[\'multi_dropdown\'] ) ) {
update_user_meta( $user_id, \'multi_dropdown\', $_POST[\'multi_dropdown\'] );
}
?>
但当然,它只保存一个值,因为我想我必须将下拉列表中选定的值保存在一个数组中。但我不知道怎么做。

有人能分享这些知识吗?

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

您需要一个开头的select标记来告诉PHP它是一个数组。以下几点应该会让你走上正轨。。。

<?php
// Display Fields 
add_action( \'show_user_profile\', \'add_multiple_choice_dropdown \' );
add_action( \'edit_user_profile\', \'add_multiple_choice_dropdown \' );

function add_multiple_choice_dropdown ( $user ) {
    $current_selections = get_user_meta( $user->ID, \'multi_dropdown\', true );
?>

<h3>Extra profile information</h3>

<table class="form-table"> 
<tr>
<th><label for="multi_dropdown" multiple="multiple">The dropdown with multiple choices</label></th>
<td>
<select name="multi_dropdown[]">
        <option value="first_choice" <?php echo ( !empty( $current_selections ) && in_array( \'first_choice\', $current_selections ) ? \' selected="selected"\' : \'\' ) ?>>First Choice</option>
        <option value="second_choice" <?php echo ( !empty( $current_selections ) && in_array( \'second_choice\', $current_selections ) ? \' selected="selected"\' : \'\' ) ?>>Second Choice</option>
        <option value="third_choice" <?php echo ( !empty( $current_selections ) && in_array( \'second_choice\', $current_selections ) ? \' selected="selected"\' : \'\' ) ?>>Third Choice</option>
</select>
<p class="description">Choose from the options above.</p>
</td>
</tr>
</table>

<?php
}

// Save fields
add_action( \'personal_options_update\', \'save_multiple_choices\' );
add_action( \'edit_user_profile_update\', \'save_multiple_choices\' );

function save_multiple_choices( $user_id )    {
    if ( isset( $_POST[\'multi_dropdown\'] ) ) {
        update_user_meta( $user_id, \'multi_dropdown\', $_POST[\'multi_dropdown\'] );
    }
}
?>

SO网友:contempoinc

修改了上述答案中的代码,以便它能够正确显示查看用户配置文件时所做的多项选择,并将“多项”属性添加到选择本身。

<?php
// Display Fields 
add_action( \'show_user_profile\', \'add_multiple_choice_dropdown \' );
add_action( \'edit_user_profile\', \'add_multiple_choice_dropdown \' );

$current_selections = get_user_meta( $user->ID, \'multi_dropdown\', true );
?>

<h3>Extra profile information</h3>

<table class="form-table"> 
<tr>
    <th><label for="multi_dropdown">The dropdown with multiple choices</label></th>
    <td>
        <select name="multi_dropdown[]" multiple>
            <option value="first_choice" <?php echo ( !empty( $current_selections ) && in_array( \'first_choice\', $current_selections ) ? \' selected="selected"\' : \'\' ) ?>>First Choice</option>
            <option value="second_choice" <?php echo ( !empty( $current_selections ) && in_array( \'second_choice\', $current_selections ) ? \' selected="selected"\' : \'\' ) ?>>Second Choice</option>
            <option value="third_choice" <?php echo ( !empty( $current_selections ) && in_array( \'second_choice\', $current_selections ) ? \' selected="selected"\' : \'\' ) ?>>Third Choice</option>
        </select>
        <p class="description">Choose from the options above.</p>
    </td>
</tr>
</table>

<?php
}

// Save fields
add_action( \'personal_options_update\', \'save_multiple_choices\' );
add_action( \'edit_user_profile_update\', \'save_multiple_choices\' );

function save_multiple_choices( $user_id )    {
    if ( isset( $_POST[\'multi_dropdown\'] ) ) {
        update_user_meta( $user_id, \'multi_dropdown\', $_POST[\'multi_dropdown\'] );
    }
}
?>

结束

相关推荐

WP_DROPDOWN_ROLES()替换选项值=CODE

我正在尝试替换插件中角色的硬编码下拉列表。目前代码如下:echo \'<select name=\"xf_user_role_\' . $id . \'\" id=\"xf_user_role_\' . $id . \'\">\'; ?> <option value=\'subscriber\' <?php echo ($xf_options[\'xf_user_role\'][$id] == \'subscriber\') ? \"sele