最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

I'm thinking this may be more of a problem with my PHP rather than my use of the settings API, but I can't seem to get Wordpress to save an array of data for a multi-select box.

The multi-select box corresponds to a custom meta field, and the select options are the custom post types that will display that meta field.

Here is the display code:

case 'select2':  
        echo "<select id='$id' style='width:15em;height:10em;' class='select$field_class' name='" . $buddha_option_name . "[$id]' multiple>";  
        foreach($choices as $item) {  

            $item = explode("|",$item);  
            $item[0] = esc_html($item[0], 'buddha_textdomain');  

            $selected = ($options[$id]==$item[1]) ? 'selected="selected"' : '';  
            echo "<option value='$item[1]' $selected>$item[0]</option>";  
        } 
        echo "</select>";  
        echo ($desc != '') ? "<br /><span class='description'>$desc</span>" : ""; 
    break;  

$choices is defined here as 'choices':

$options[] = array(  
    "section" => "custom_meta",  
    "id"      => BUDDHA_SHORTNAME . "_meta_email",  
    "title"   => __( 'Email Meta Box', 'buddha_textdomain' ),  
    "desc"    => __( 'Select post types to have custom email meta box.', 'buddha_textdomain' ),  
    "type"    => "select2",  
    "std"    => print_r($buddha_option_name[$id]),  
    "choices" => array( __('Posts','buddha_textdomain') . "|post", __('Pages','buddha_textdomain') . "|page", __('Faculty/Staff','buddha_textdomain') . "|staff", __('FAQ','buddha_textdomain') . "|faq", __('Documents','buddha_textdomain') . "|docs", __('Courses','buddha_textdomain') . "|courses" )  
);

And the validation code:

case 'select2': 
                // process $select_values 
                    $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']] : '' ); 
            break; 

Currently, if I select more than one value from the multi-select field, WP will only save one value (usually the first alphabetical value). I need it to save more than one value in the array.

I thought adding [] to the end of [$id] might help, but no.

I'm thinking this may be more of a problem with my PHP rather than my use of the settings API, but I can't seem to get Wordpress to save an array of data for a multi-select box.

The multi-select box corresponds to a custom meta field, and the select options are the custom post types that will display that meta field.

Here is the display code:

case 'select2':  
        echo "<select id='$id' style='width:15em;height:10em;' class='select$field_class' name='" . $buddha_option_name . "[$id]' multiple>";  
        foreach($choices as $item) {  

            $item = explode("|",$item);  
            $item[0] = esc_html($item[0], 'buddha_textdomain');  

            $selected = ($options[$id]==$item[1]) ? 'selected="selected"' : '';  
            echo "<option value='$item[1]' $selected>$item[0]</option>";  
        } 
        echo "</select>";  
        echo ($desc != '') ? "<br /><span class='description'>$desc</span>" : ""; 
    break;  

$choices is defined here as 'choices':

$options[] = array(  
    "section" => "custom_meta",  
    "id"      => BUDDHA_SHORTNAME . "_meta_email",  
    "title"   => __( 'Email Meta Box', 'buddha_textdomain' ),  
    "desc"    => __( 'Select post types to have custom email meta box.', 'buddha_textdomain' ),  
    "type"    => "select2",  
    "std"    => print_r($buddha_option_name[$id]),  
    "choices" => array( __('Posts','buddha_textdomain') . "|post", __('Pages','buddha_textdomain') . "|page", __('Faculty/Staff','buddha_textdomain') . "|staff", __('FAQ','buddha_textdomain') . "|faq", __('Documents','buddha_textdomain') . "|docs", __('Courses','buddha_textdomain') . "|courses" )  
);

And the validation code:

case 'select2': 
                // process $select_values 
                    $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']] : '' ); 
            break; 

Currently, if I select more than one value from the multi-select field, WP will only save one value (usually the first alphabetical value). I need it to save more than one value in the array.

I thought adding [] to the end of [$id] might help, but no.

Share Improve this question asked Jun 26, 2013 at 19:15 SpartacusSpartacus 3973 silver badges15 bronze badges 4
  • What's $id? And why are using print_r in an assign context? – TheDeadMedic Commented Jun 27, 2013 at 6:06
  • $id is the same as "id" => BUDDHA_SHORTNAME . "_meta_email", it's defined in another function. And I don't really know why I did print_r there, doesn't make much sense does it? – Spartacus Commented Jun 27, 2013 at 18:47
  • Did you make it work? Addin [] at the end of name attribute helped me. Double check the name attribute. Do you check the mySQL db what settings are stored? – Radek Commented Aug 22, 2013 at 1:10
  • Unrelated to your question: I think you've confused esc_html() and esc_html__() in your display code. The former accepts only one argument; the latter accepts a second argument, the text domain. – Pat J Commented Jul 6, 2018 at 14:44
Add a comment  | 

1 Answer 1

Reset to default 1

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.

发布评论

评论列表(0)

  1. 暂无评论