• i am trying to use selected() function in my select box, inside my wordpress theme options page. The problem is with numbers it works but it is not working comparing strings. Note that below if nothing is selected, automatically default is selected in the select box

    Theme Style <select name="corpocrat-options[theme-style]">
      <option value="" <?php selected($corpocrat_options['theme_style'],""); ?>>Default</option>
      <option value="black" <?php selected($corpocrat_options['theme_style'],"black"); ?>>Black</option>
      <option value="blue" <?php selected($corpocrat_options['theme_style'],"blue"); ?>>Blue</option>
      <option value="grey" <?php selected($corpocrat_options['theme_style'],"grey"); ?>>Grey</option>
      <option value="grunge" <?php selected($corpocrat_options['theme_style'],"grunge");  ?>>Grunge</option>
    </select>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Everything appears correct, but I can’t conceive a scenario where numbers work but strings don’t. Try var_dump($corpocrat_options['theme_style']); All I can imagine is the value is not what you are expecting.

    How are you saving the field value and then how are you assigning it to $corpocrat_options['theme_style']?

    Thread Starter olddocks

    (@olddocks)

    @bcworkz, Thank you very much for the followup,i am using the good old way of doing selected..

    <option value="blue" <?php if($corpocrat_options['theme-style']=="blue") { echo 'selected="selected"';} ?>>Blue</option>

    I am happy with the code but the wordpress theme specifications indicate that themes are required to use the selected() function, which is why i am concerned.

    Moderator bcworkz

    (@bcworkz)

    Well, if that code is working it completely destroys any theories I had about what’s the problem ??

    There isn’t that much to selected(). It’s comparison is basically (string)$corpocrat_options['theme-style']===(string)"blue" so really no different at all. Very strange!

    I now have to believe some sort of type conversion is going awry in the core code as values are passed. This wouldn’t be a WP bug, but some strange quirk about your PHP installation. It would take some detailed debugging in core to pinpoint what’s going wrong. You of course couldn’t rewrite core code but it may indicate what you can change in your code to get things working, though there’s technically nothing wrong with your code.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Issues with select box in options page?’ is closed to new replies.