这是一个相当基本的例子,但它应该会让你走上你的路;
function my_upload_field()
{
echo '<input type="file" name="my_upload_field" />';
}
add_action('init', create_function('',
'add_meta_box("my_upload_field", "Upload File", "my_upload_field", "post");'));
function handle_upload_field($post_ID, $post)
{
if (!empty($_FILES['my_upload_field']['name'])) {
$upload = wp_handle_upload($_FILES['my_upload_field']);
if (!isset($upload['error'])) {
// no errors, do what you like
}
}
}
add_action('wp_insert_post', 'handle_upload_field', 10, 2);
查看上的codex
add_meta_box
然后看看
wp_handle_upload()
(第239行)
wp-admin/includes/file.php
从3.0)开始获取更多信息。