WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm building a theme options panel and so far been successful, except with the "Select" type option. This is my code:

array( "name" => "Transition",
    "desc" => "Choose the type of transition.",
    "id" => $shortname."_transition",
    "type" => "select",
    "std"   => '1',
    "options" => array("0" => "None", "1" => "Fade", "2" => "Slide Top", "3" => "Slide Right"),

Now, my select code is this:

<select name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
<?php foreach ($value['options'] as $option) { ?><option <?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option><?php } ?>

Now, when I pull the option into the theme, it outputs the option name (fade, Slide Top...etc) instead of the value (0, 1, 2, 3).

I'm guessing here that perhaps I'm missing the value="" from the /option> code? any help would be appreciated, thank you.

Edit: figured it out, edited the second code to this:

<?php foreach ($value['options'] as $key=>$option) { ?><option value="<?php echo $key; ?>" <?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option><?php } ?></select>
share|improve this question
Please don't link to outside code. Always paste everything in here. – kaiser Feb 12 '13 at 18:59
My apologies, I tried pasting the code here and it kept disappearing. Thanks for the fix. – user156 Feb 12 '13 at 19:09
You should post your solution as an answer and accept it. – s_ha_dum Feb 12 '13 at 19:30
Tried that, got this message: "Users with less than 10 reputation can't answer their own question for 8 hours after asking. You can answer in 7 hours. Until then please use comments, or edit your question instead." Will post it tomorrow I guess. – user156 Feb 12 '13 at 19:38
up vote 1 down vote accepted

There're several things to note:

  • get_settings() is deprecated and get_option() should be used instead
  • WordPress comes with a function named select() that takes three arguments: Saved value, looped value, echo
  • You're better off saving your key, than the HTML capable "title" element in your options. It's much safer to save in the DB an easier to compare as the output doesn't change. See the comparison with $key, as I assume that you change the saved value to the actual value attribute. So instead of

    "0" => "None", "1" => "Fade", "2" => "Slide Top", "3" => "Slide Right"

    You'd set better keys:

    $effects = array(
         'none' => 'None'
        ,'fade' => 'Fade'
        ,'slide-top' => 'Slide Top'
        ,'slide-right' => 'Slide Right'

The best you could do is taking the real values that your slider JavaScript function takes and take those as keys. This way you can simply echo them and are done.

Another advantage is, that you could make your values translatable without affecting functionality: __( 'Slide Right', 'your_textdomain' ).

Here's the actual loop:

$html = '';
foreach ( $value['options'] as $key => $option )
    $html .= sprintf(
         '<option value="%s">%s</option>'
        ,selected( get_option( $value['id'] ), $key, false )
echo "<select>{$html}</select>";
share|improve this answer

Your Answer


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.