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'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
1  
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
1  
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
add comment

1 Answer

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>'
        ,$key
        ,selected( get_option( $value['id'] ), $key, false )
        ,$option
    );
}
echo "<select>{$html}</select>";
share|improve this answer
add comment

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.