WordPress 3.0 custom post type with upload

荒凉一梦 提交于 2020-01-03 15:37:01

问题


Is there a way to insert one (or more) upload field on a custom post type edition page?

I don't want to use the midia gallery with all the fields and stuff.


回答1:


This is a fairly basic example, but it should get you on your way;

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);

Check out the codex on add_meta_box, and take a look at wp_handle_upload() (line 239 wp-admin/includes/file.php as of 3.0) for more information.



来源:https://stackoverflow.com/questions/3249666/wordpress-3-0-custom-post-type-with-upload

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!