查看此函数的第一行:
function media_handle_upload(
    $file_id, 
    $post_id, 
    $post_data = array(), 
    $overrides = array( \'test_form\' => false )
) 
{
    $time = current_time(\'mysql\');
    if ( $post = get_post($post_id) ) {
        if ( substr( $post->post_date, 0, 4 ) > 0 )
            $time = $post->post_date;
    }
    $name = $_FILES[$file_id][\'name\'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
 时间取自发布日期。你可以绕过
media_handle_upload() 和使用
wp_handle_upload() 直接地
更新我没有时间写一个完全独立的示例,但这里是我的主题选项类的摘录。它可以处理上传,生成不同大小的图像和附件id。它由通用保存功能调用,可以一次性处理多个上传的文件。
/**
 * Saves uploaded files in media library and the corresponding id in option field.
 *
 * @return void
 */
protected function handle_uploads()
{
    if ( ! isset ( $_FILES ) or empty ( $_FILES ) )
    {
        return;
    }
    foreach ( $_FILES as $file_key => $file_arr )
    {
        // Some bogus upload.
        if ( ! isset ( $this->fields[$file_key] )
            or empty ( $file_arr[\'type\'] )
        )
        {
            continue;
        }
        if ( ! $this->is_allowed_mime( $file_key, $file_arr ) )
        {
            set_theme_mod( $file_key . \'_error\', \'wrong mime type\' );
            continue;
        }
        // The file is allowed, no error until now and the type is correct.
        $uploaded_file = wp_handle_upload(
            $file_arr
        ,   array( \'test_form\' => FALSE )
        );
        // error
        if ( isset ( $uploaded_file[\'error\'] ) )
        {
            set_theme_mod( $file_key . \'_error\', $uploaded_file[\'error\'] );
            continue;
        }
        // add the file to the media library
        // Set up options array to add this file as an attachment
        $attachment = array(
            \'post_mime_type\' => $uploaded_file[\'type\']
        ,   \'post_title\'     => $this->get_media_name(
                                    $file_key, $uploaded_file[\'file\']
                                )
        );
        // Adds the file to the media library and generates the thumbnails.
        $attach_id = wp_insert_attachment(
            $attachment
        ,   $uploaded_file[\'file\']
        );
        $this->create_upload_meta( $attach_id, $uploaded_file[\'file\'] );
        // Update the theme mod.
        set_theme_mod( $file_key, $attach_id );
        remove_theme_mod( $file_key . \'_error\' );
    }
}
/**
 * Adds meta data to the uploaded file
 *
 * @param  int $attach_id
 * @param  string $file
 * @return void
 */
protected function create_upload_meta( $attach_id, $file )
{
    // Create meta data from EXIF fields.
    require_once ABSPATH . \'wp-admin/includes/image.php\';
    $attach_data = wp_generate_attachment_metadata(
        $attach_id
    ,   $file
    );
    wp_update_attachment_metadata($attach_id,  $attach_data);
}
 一些注意事项:
- is_allowed_mime()检查中设置的MIME类型- $fields在我的控制器中设置。例如,favicon的MIME类型为- image/x-icon或- image/vnd.microsoft.icon.
- get_media_name()可以为附件使用预定义的名称(例如徽标)在函数中,您可能希望返回- $attach_id并将其用于post meta字段或类似的内容