How can I save a multiple select array with the settings API for a plug-in options page?

So, it looks like two things are going on here:

echo "<select id='$id' style="width:15em;height:10em;" class="select$field_class" name="" . $buddha_option_name . "[$id]" multiple>";  

As noted by @Radek, you could have made the name of the select box an array by using [] – e.g. name="my_option_name[]" – this will result in the filed $_POST['my_option_name'] being an array of selected options that you can then store.

A closer look at your code has me stumped:

$select_values = array(); 
foreach ($option['choices'] as $k => $v) { 
    // explode the connective 
    $pieces = explode("|", $v); 

    $select_values[] = $pieces[1]; 
} 
// check to see if selected value is in our approved array of values! 
$valid_input[$option['id']] = (in_array( $input[$option['id']], $select_values) ? $input[$option['id']] : '' ); 

You’re only ever saving one value to $valid_input[$option['id']] – is your switch statement in a loop itself? If so, try changing the last line to this:

$valid_input[$option['id']][] = (in_array( $input[$option['id']], $select_values) ? $input[$option['id']] : '' );

Note the [] before the assignment operator. When you save $valid_input, it should treat the value of $valid_input[$option['id']] as an array.

Leave a Comment