I want to display a select dropdown field in the post meta. How can I validate the input before saving it?
Currently, I am simply saving without validating like this:
if ( isset( $_POST['select_meta'] ) ) {
update_post_meta( $post->ID, 'select_meta', $_POST['select_meta'] );
}
Here's the code I'm using to display the meta field:
$select_items = ('one', 'two', 'three');
$select_meta = get_post_meta( $post->ID, 'select_meta', true ); ?>
<select id="select_meta" name="select_meta">
<?php
if($select_items){
foreach ($select_items as $item){?>
<option <?php selected( $item == $select_meta ); ?> value="<?php echo $item; ?>"><?php echo $item ?></option>
<?php
}
}
?>
</select>