In a select
list, the correct way to "select" an option is with selected="selected"
, not default="default"
. Changing your 'None' option to the following should fix it:
echo '<option value="" selected="selected"> None... </option>';
However, you're not dynamically creating your select
list, you're just outputting a full list and you'll run into issues selecting the other values too. To fix this, if the options won't be changing, you can put them in an array to loop through them. You can modify your rating_select_cb()
method with the following new code to achieve this:
function rating_select_cb( $post ) {
global $wpdb;
$value = get_post_meta($post->ID, 'rating', true);
echo '<div class="misc-pub-section misc-pub-section-last"><span id="timestamp"><label>Select a rating:<br></label>';
// build an array of each available rating
$ratings = array(
1 => 'G — Suitable for all audiences',
2 => 'PG — Possibly offensive, usually for audiences 13 and above',
3 => 'R — Intended for adult audiences above 17',
4 => 'X — Even more mature than above'
);
echo '<select name="rating">';
echo '<option value=""' . ((($value == '') || !isset($ratings[$value])) ? ' selected="selected"' : '') . '> None... </option>';
// output each rating as an option
foreach ($ratings as $id => $text) {
echo '<option value="' . $id . '"' . (($value == $id) ? ' selected="selected"' : '') . '">' . $text. '</option>';
}
echo '</select>';
echo '</span></div>';
}
This method will default-select the "None" option if the selected $value
is not in the available list of ratings. After that, it will loop through each rating and output it as an option. If the rating's $id
matches the selected $value
, it will be marked as selected.