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 using the WordPress selected() function to display the current option in a dropdown list. The issue I am having is that for some reason WordPress is printing out selected='selected' above the form in addition to in the actual <option> elements. This is how I am using the selected() function.

<select name='gender'>
<option value='male' " . selected( $values['gender'] , 'male') . ">Male</option>
<option value='female' " . selected( $values['gender'] , 'female') . ">Female</option>
<option value='other' " . selected( $values['gender'] , 'other') . ">Other</option>         

Removing the markup above eliminates the selected='selected'which is appearing completely outside of the <form> element.

share|improve this question

The selected function causes immediate output, it does not return a string. Therefore you can't use it like that, in a string building mode.

If you want it to return a string instead of immediately outputting the text, pass false to the $echo parameter.

$string = "
<select name='gender'>
<option value='male' " . selected( $values['gender'] , 'male', false) . ">Male</option>
<option value='female' " . selected( $values['gender'] , 'female', false) . ">Female</option>
<option value='other' " . selected( $values['gender'] , 'other', false) . ">Other</option>         
share|improve this answer
nails it, on the correct assumption that the form markup was part of a constructed string. – Chip Bennett Jul 7 '11 at 18:21
Thank you! I had been looking at the Codex page (codex.wordpress.org/Function_Reference/selected) which makes no reference to that last parameter. – lsparks Jul 7 '11 at 19:59
FYI: The selected() function returns a value preceeded by a space, so there's no need to add additional space before the function call. – t31os Jul 7 '11 at 20:46

Where are your PHP tags?

<select name="gender">
<option value="male" <?php selected( $values['gender'] , 'male'); ?>>Male</option>
<option value="female" <?php selected( $values['gender'] , 'female'); ?>>Female</option>
<option value="other" <?php selected( $values['gender'] , 'other'); ?>>Other</option>         

Unless you're somehow wrapping the whole thing in an echo that isn't displayed in your code snippet?

Also, probably entirely a matter of personal preference, but I always put HTML attributes in double-quotes, and PHP attributes in single quotes, in order to help avoid mixing them up in mixed PHP/HTML markup.

share|improve this answer
The whole statement is wrapped in double quotes and returned from a method that displays the form when called. The quotes are they way they are because I reversed them from my normal method on the off chance that it would make a difference. It did not. – lsparks Jul 7 '11 at 18:11

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.