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 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>         
</select>

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

share|improve this question

2 Answers 2

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>         
</select>
";
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
1  
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>         
</select>

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

 
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.