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.

I am trying to create a multi-select category list in a theme options page and everything shows up but when i save the options only the last option from the multi-select box that is selected gets saved to the database.

Does someone know what I'm doing wrong perhaps?

Here's bits and pieces of the code I'm currently using:

<?php

$themename_themename = "themename";
$themename_shortname = "themename";
$themename_option_group = $themename_shortname.'_theme_option_group';
$themename_option_name = $themename_shortname.'_theme_options';


// WordPress Categories via an Array
    $wp_cats = array();  
    $categories = get_categories('hide_empty=0&orderby=name');
    foreach ($categories as $category_list) {
        $wp_cats[$category_list->cat_ID] = $category_list->cat_name;
    }
    $categories_tmp = array_unshift($wp_cats, "Select a category:");

// Create theme options
global $themename_settings;

$themename_settings = array (

    // Multi-select
    array(  "name" => __( 'Cat List','themename'), 'id' => $themename_shortname.'_list_cats', 'type' => 'multi-select',
            "desc" => __( 'Multi Select Unorderd Lists in content  area.','themename'),
            'std' => '',
            "value" => $wp_cats ),

);

function themename_settings_page() {


    case 'multi-select':        // Multi-select ?>

        <div class="options_input options_select">
            <div class="options_desc"><?php echo $value['desc']; ?></div>
        <span class="labels"><label for="<?php echo $themename_option_name.'['.$valueid.']'; ?>"><?php echo $value['name']; ?></label></span>
            <select name="<?php echo $themename_option_name.'['.$valueid.']'; ?>" id="<?php echo $themename_option_name.'['.$valueid.']'; ?>" multiple="multiple"  style="height:150px; min-width: 200px;">

            <?php
                $multi_setting = $options[$valueid];
                foreach ($value['value'] as $category_list) {
            ?>
                    <option value="<?php echo $category_list ?>" <?php if($multi_setting != '') { selected($options[$valueid] == $category_list );} else {  selected($options[$valueid] == $value['std']);} ?>><?php echo $category_list; ?></option>
                <?php } ?>
            </select>
        </div>
        <?php
    break;

 } // --themename_settings_page

Here is a temp. screenshot of what is going on: Screenshot

Any help would be very appreciated.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

This may be an HTML issue. For a multiple select, the name of needs to end in "[]", to signify an array. So try changing:

<select name="<?php echo $themename_option_name.'['.$valueid.']'; ?>" id="<?php echo $themename_option_name.'['.$valueid.']'; ?>" multiple="multiple"  style="height:150px; min-width: 200px;">

to

<select name="<?php echo $themename_option_name.'['.$valueid.']'; ?>[]" id="<?php echo $themename_option_name.'['.$valueid.']'; ?>" multiple="multiple"  style="height:150px; min-width: 200px;">

So you should end up with:

<select name="hurst_theme_options[hurst_content_bullet_cats][]" id="...
share|improve this answer
    
Thanks for your answer Devin, that is something i missed but im still having the same problem. –  Wendy99966 Aug 26 '12 at 15:51

Thanks for the help Devin, this is the answer for what ended up working for me in case anyone else stumbles upon this.

<?php
case 'multi-select': // Multi-select ?>

    <div class="options_input options_select">
        <div class="options_desc"><?php echo $value['desc']; ?></div>
        <span class="labels"><label for="<?php echo $themename_option_name.'['.$valueid.']'; ?>"><?php echo $value['name']; ?></label></span>
        <select name="themename_theme_options[themename_content_bullet_cats][]" id="<?php echo $themename_option_name.'['.$valueid.']'; ?>" multiple="multiple" size="8">
            <?php /* Get the list of categories */ 
            $cats_selected = $options[$valueid];
            $categories = get_categories();
            foreach ( $categories as $category) :
            ?>
            <option value="<?php echo $category->cat_ID; ?>" <?php if ( $cats_selected && in_array( $category->cat_ID, $cats_selected ) ) { echo 'selected="selected"'; }?>><?php echo $category->cat_name; ?></option>
            <?php endforeach; ?>
        </select>
    </div>
<?php
break; 
share|improve this answer

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.