Php WordPress文件;上传“;来自插件

Php WordPress文件;上传“;来自插件,php,wordpress,Php,Wordpress,我正在尝试创建插件,将帖子导入WordPress。导入的文章(XML)包含“图像名称”属性,并且此图像已上载到服务器 然而,我想让WordPress发挥它的“魔力”,并将图像导入系统(创建缩略图,将其附加到帖子,将其置于wp uploads目录方案下)。。。我找到了函数media\u handle\u upload($file\u id、$post\u id、$post\u data、$overrides),但它需要用实际上传填充数组$\u文件(我不上传文件-文件已经放在服务器上),所以它不是很

我正在尝试创建插件,将帖子导入WordPress。导入的文章(XML)包含“图像名称”属性,并且此图像已上载到服务器

然而,我想让WordPress发挥它的“魔力”,并将图像导入系统(创建缩略图,将其附加到帖子,将其置于wp uploads目录方案下)。。。我找到了函数
media\u handle\u upload($file\u id、$post\u id、$post\u data、$overrides)
,但它需要用实际上传填充数组$\u文件(我不上传文件-文件已经放在服务器上),所以它不是很有用

你有什么提示如何进行吗


谢谢

检查以下脚本以了解想法。(它确实有效。)


图像是否放置在正确的上载文件夹中,或者您希望将其移动到正确的位置,然后链接到帖子??它仅通过ftp放置到服务器,不在wp上载文件夹中。。。我想wordpress显示媒体库中的图像(与帖子链接),并将其移动到相应的目录。。。
    $title = 'Title for the image';
    $post_id = YOUR_POST_ID_HERE; // get it from return value of wp_insert_post
    $image = $this->cache_image($YOUR_IMAGE_URL);
    if($image) {
        $attachment = array(
            'guid' => $image['full_path'],
            'post_type' => 'attachment',
            'post_title' => $title,
            'post_content' => '',
            'post_parent' => $post_id,
            'post_status' => 'publish',
            'post_mime_type' => $image['type'],
            'post_author'   => 1
        );

        // Attach the image to post
        $attach_id = wp_insert_attachment( $attachment, $image['full_path'], $post_id );
        // update metadata
        if ( !is_wp_error($attach_id) )
        {
            /** Admin Image API for metadata updating */
            require_once(ABSPATH . '/wp-admin/includes/image.php');
            wp_update_attachment_metadata
            ( $attach_id, wp_generate_attachment_metadata
            ( $attach_id, $image['full_path'] ) );
        }
    }

function cache_image($url) {
    $contents = @file_get_contents($url);
    $filename = basename($url);
    $dir = wp_upload_dir();
    $cache_path = $dir['path'];
    $cache_url = $dir['url'];

    $image['path'] = $cache_path;
    $image['url'] = $cache_url;

    $new_filename = wp_unique_filename( $cache_path, $filename );
    if(is_writable($cache_path) && $contents)
    {
        file_put_contents($cache_path . '/' . $new_filename, $contents);

        $image['type'] = $this->mime_type($cache_path . '/' . $new_filename); //where is function mime_type() ???

        $image['filename'] = $new_filename;
        $image['full_path'] = $cache_path . '/' . $new_filename;
        $image['full_url'] = $cache_url . '/' . $new_filename;
        return $image;
    }
    return false;
}