1

I use this code to create the dropdown and then save the dropdown chosen values:

<?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'] );
}
?>

But of course, it doesn't save but one value, because I guess I have to save the selected values of the dropdown in an array. But I don't know how to do this.

Can someone please share the knowledge?

2
  • Where's your opening <select /> tag? Commented Apr 4, 2015 at 16:29
  • Hi! You're right! I just forgot to copy (from the code I tested on my computer) the line with the opening <select> tag. I edited my code above. I didn't realize I left it out, because I was focused on the saving part I was having issues with.
    – Ami
    Commented Apr 8, 2015 at 17:22

2 Answers 2

1

You will need an opening select tag that tells PHP it is an array. Something along the lines of the following should get you on your way...

<?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'] );
    }
}
?>
5
  • Thank you Mike, for your answer! I tried the code you suggested, but when I use it I only get a blank screen on my site, no matter what page I am trying to access in the admin interface. I am not sure why...But I suspect it has something to do with the "in_array()" function. Sorry if the issue is something really obvious, but I am just starting to learn php.
    – Ami
    Commented Apr 8, 2015 at 18:56
  • @Ami there was a missing close bracket on the save_multiple_choices function. I have edited the answer and added it in.
    – Mike
    Commented Apr 9, 2015 at 19:12
  • Thank you! Indeed, now it works, it's saving the values chosen in the dropdown in an array in the database. But do you know how can I also make the chosen values stay selected in the user profile dropdown after saving them? Because it's confusing to not know if you chose something previously or what you chose for each user. And currently, to find out you have to go in the database. (Also, I think you should also add "multiple" to this line: <select name="multi_dropdown[]" multiple> in order to actually make a multiple selection.)
    – Ami
    Commented Apr 11, 2015 at 13:03
  • Yes you will need 'multiple' in the select tag. I thought the selection would work with the in_array statement as it was but thinking about it, it probably won't... I have edited above and it should now work
    – Mike
    Commented Apr 11, 2015 at 13:24
  • The code from before and the current one seem to have the same result. But I marked your answer and chose it, because it answers my initial issue: saving the selections in an array in the database. Now only displaying the selections in the user profile still is a little issue. But the selections are saved correctly in the database. Thank you so much and have a nice weekend!
    – Ami
    Commented Apr 11, 2015 at 18:28
0

Modified code from the above answer so it will properly show the multiple selections made when viewing the users profile, as well as added the "multiple" attribute to the select itself.

<?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'] );
    }
}
?>

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.