我的脚本无法保存下拉框的值。我找不到有关此主题的任何有用文档,因此我决定询问StackExchange社区。
function _cc_add_our_attachment_meta() {
add_meta_box( \'cc-license-attachment-meta-box\', \'License of the Attachment\', \'_cc_our_attachment_meta_box_callback\', \'attachment\', \'normal\', \'low\');
}
add_action( \'admin_init\', \'_cc_add_our_attachment_meta\' );
function _cc_our_attachment_meta_box_callback() {
global $post;
$value = get_post_meta($post->ID, \'_license\', false);
if($value == \'\')
{
$value = \'0\';
}
?>
<p>Choose a license for this file</p>
<select name="license_id">
<option value="0" <?php selected( \'0\', $value ); ?>>License 1</option>
<option value="1" <?php selected( \'1\', $value ); ?>>License 2</option>
<option value="2" <?php selected( \'2\', $value ); ?>>License 3</option>
<option value="3" <?php selected( \'3\', $value ); ?>>License 4</option>
<option value="4" <?php selected( \'4\', $value ); ?>>License 5</option>
<option value="5" <?php selected( \'5\', $value ); ?>>License 6</option>
</select>
<?php
}
function _cc_attachment_save_our_attachment_meta($post_ID) {
if( isset( $_POST[\'license_id\'] ) ) {
$license = $_POST[\'license_id\'];
switch($license)
{
case 0: $license = \'0\';
case 1: $license = \'1\';
case 2: $license = \'2\';
case 3: $license = \'3\';
case 4: $license = \'4\';
case 5: $license = \'5\';
default: $license = \'0\';
}
update_post_meta( $post_ID, \'_license\', $license );
}
}
add_action(\'edit_attachment\', \'_cc_attachment_save_our_attachment_meta\', 10, 1);