我想在post meta中显示一个select下拉字段。如何在保存之前验证输入?
目前,我只是简单地保存而不进行验证,如下所示:
if ( isset( $_POST[\'select_meta\'] ) ) {
update_post_meta( $post->ID, \'select_meta\', $_POST[\'select_meta\'] );
}
下面是我用来显示元字段的代码:
$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>
SO网友:fuxia
分隔函数中允许的值:
function get_allowed_values()
{
return array(\'one\', \'two\', \'three\');
}
在元数据库中:
foreach ( get_allowed_values() as $item) {}
在验证处理程序中:
if ( isset( $_POST[\'select_meta\'] )
and in_array( $_POST[\'select_meta\'], get_allowed_values() )
)