我为我的 Wordpress 主题创建了一个自定义选项页面,我需要添加一个文件输入字段,通过该字段可以上传 MP4 视频(~15Mb)。问题是$_FILES
变量总是空的。
/**
* Custom theme form
*/
function theme_option_page() {
?>
<div class="wrap">
<h1>Cusstom Theme Options</h1>
<form method="post" action="options.php">
<?php
settings_fields("theme-options-grp");
do_settings_sections("theme-options");
submit_button();
?>
</form>
</div>
<?php
}
/**
* Custom theme settings
*/
function add_theme_menu_item() {
add_theme_page(
"Theme Options",
"Theme Options",
"manage_options",
"theme-options",
"theme_option_page",
null,
99
);
}
add_action('admin_menu', 'add_theme_menu_item');
/**
* Theme section
*/
function theme_section_description() {
echo '<p>Options for this theme</p>';
}
/**
* File upload
*/
function display_product_teaser()
{
?>
<input type="file" name="product_teaser" accept=".mp4" value="<?php echo get_option('product_teaser'); ?>" />
<?php
}
/**
* Handler for the uploaded file
*/
function handle_product_teaser_upload($option) {
// $_FILES is empty here !!!
if (!empty($_FILES["product_teaser"]["tmp_name"])) {
$urls = wp_handle_upload($_FILES["product_teaser"], array('test_form' => false));
$temp = $urls["url"];
return $temp;
}
return $option;
}
/**
* Add theme settings fields
*/
function custom_theme_settings() {
add_settings_field("product_teaser", "Product teaser (mp4)", "display_product_teaser", "theme-options", "first_section");
register_setting( 'theme-options-grp', 'product_teaser', 'handle_product_teaser_upload' );
}
add_action('admin_init', 'custom_theme_settings');
我尝试添加enctype="multipart/form-data"
到表单,但是当我提交它时,我最终进入了wp-admin/options.php,其中显示了所有设置,而不是返回到主题的自定义选项表单。