WordPress selected() does not save value in Dropdown-Box

I am trying to use the selected() function from WordPress but I do not get it to work. I created a metabox and have a dropdown box there. If a user saves the page, the value is submitted to the database and is working fine.

The only thing that I do not get to work is, that the selected value is also displayed at the metabox once I save the page. It always shows the first entry “Default”.

My code:

<?php $metaheader = get_post_meta($post_id, 'meta-option-header', true); ?>

<select name="meta-option-header" id="meta-option-header" class="form-control" title="Show Header">
    <option value="0" <?php selected( $metaheader, '0', false ); ?>>Default</option>
    <option value="1" <?php selected( $metaheader, '1', false ); ?>>Yes</option>
    <option value="2" <?php selected( $metaheader, '2', false ); ?>>No</option>
</select>

Any help would be appreciated.

Thanks!

Read more here: WordPress selected() does not save value in Dropdown-Box

Leave a Reply

Your email address will not be published. Required fields are marked *