I’m trying to figure out how to get the results from a select box option and apply them to my theme. When I use a text input or color picker it works fine. But when I use a select box, nothing is changing. Here’s the option:

$options['color_scheme'] = array(
        'name' => __('Color Scheme', 'text-domain'),
        'desc' => __('Select a color scheme.', 'text-domain'),
        'id' => 'color_scheme',
        'std' => 'blue',
        'type' => 'select',
        'class' => 'mini',
        'options' => array('blue', 'yellow'));

This code below is where I’m trying to output certain option based on the selection:

if ( of_get_option('color_scheme') == "blue") {
                $output .= "#header {background:#000066;}\n";
            }
    else if ( of_get_option('color_scheme') == "yellow") {
                $output .= "#header {background:#FFFF66;}\n";
            }

if ($output <> '') {
            $output = "<!-- Custom Styling -->\n<style type=\"text/css\">\n" . $output . "</style>\n";
            echo $output;
        }

1 Answer
1

I use the same options framework plugin and this is how I specify a select menu using this framework;

$test_array = array(
    'one' => __('One', 'options_framework_theme'),
    'two' => __('Two', 'options_framework_theme'),
    'three' => __('Three', 'options_framework_theme'),
    'four' => __('Four', 'options_framework_theme'),
    'five' => __('Five', 'options_framework_theme')
);

$options[] = array(
    'name' => __('Select a Tag', 'options_check'),
    'desc' => __('Passed an array of tags with term_id and term_name', 'options_check'),
    'id' => 'example_select_tags',
    'type' => 'select',
    'options' => $test_array);

Note that the options parameter accepts;

'options' => array('value' => 'label')

Where as in your instance you have only specified the label using;

'options' => array('blue', 'yellow')

Which means blue has a value of 0 and yellow has a value of 1.

Therefore the following is incorrect,

if ( of_get_option('color_scheme') == "blue") 

As it should be,

if ( of_get_option('color_scheme') == 0) //0 for blue 1 for yellow

If you want to check by a value name of blue or yellow then your array should look like,

'options' => array(
    'blue' => 'Blue',
    'yellow' => 'Yellow'
 )

Leave a Reply

Your email address will not be published. Required fields are marked *