MEDIA_SIDELOAD_IMAGE文件名?

时间:2011-10-04 作者:FLX

我正在使用media\\u sideload\\u image从web下载图像并存储它们。然而,问题是youtube上的图片总是被称为“hqdefault.jpg”,这意味着它将覆盖现有的图片。有三种方法可以解决此问题:

如何更改附件结构,使其包含postname?(当前:http://example.com/wp-content/uploads/2011/10/hqdefault-40x40.jpg)

1 个回复
最合适的回答,由SO网友:somatic 整理而成

当我开始使用media_sideload_image(), 我发现它非常无用,因为它返回了一个完整的html标记,而不是附件ID,所以我创建了自己的版本,它复制了media_sideload_image() 并添加了一些非常有用的内容,例如:

以全新的文件名保存附带加载的文件,使提交的图像不仅仅是附件,而是Post\\u缩略图(特色图像)

  • 能够指定各种附件元数据,只需调用下面的函数,而不是media_sideload_image()

    /**
     * Download an image from the specified URL and attach it to a post.
     * Modified version of core function media_sideload_image() in /wp-admin/includes/media.php  (which returns an html img tag instead of attachment ID)
     * Additional functionality: ability override actual filename, and to pass $post_data to override values in wp_insert_attachment (original only allowed $desc)
     *
     * @since 1.4 Somatic Framework
     *
     * @param string $url (required) The URL of the image to download
     * @param int $post_id (required) The post ID the media is to be associated with
     * @param bool $thumb (optional) Whether to make this attachment the Featured Image for the post (post_thumbnail)
     * @param string $filename (optional) Replacement filename for the URL filename (do not include extension)
     * @param array $post_data (optional) Array of key => values for wp_posts table (ex: \'post_title\' => \'foobar\', \'post_status\' => \'draft\')
     * @return int|object The ID of the attachment or a WP_Error on failure
     */
    function somatic_attach_external_image( $url = null, $post_id = null, $thumb = null, $filename = null, $post_data = array() ) {
        if ( !$url || !$post_id ) return new WP_Error(\'missing\', "Need a valid URL and post ID...");
        require_once( ABSPATH . \'wp-admin/includes/file.php\' );
        // Download file to temp location, returns full server path to temp file, ex; /home/user/public_html/mysite/wp-content/26192277_640.tmp
        $tmp = download_url( $url );
    
        // If error storing temporarily, unlink
        if ( is_wp_error( $tmp ) ) {
            @unlink($file_array[\'tmp_name\']);   // clean up
            $file_array[\'tmp_name\'] = \'\';
            return $tmp; // output wp_error
        }
    
        preg_match(\'/[^\\?]+\\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/\', $url, $matches);    // fix file filename for query strings
        $url_filename = basename($matches[0]);                                                  // extract filename from url for title
        $url_type = wp_check_filetype($url_filename);                                           // determine file type (ext and mime/type)
    
        // override filename if given, reconstruct server path
        if ( !empty( $filename ) ) {
            $filename = sanitize_file_name($filename);
            $tmppath = pathinfo( $tmp );                                                        // extract path parts
            $new = $tmppath[\'dirname\'] . "/". $filename . "." . $tmppath[\'extension\'];          // build new path
            rename($tmp, $new);                                                                 // renames temp file on server
            $tmp = $new;                                                                        // push new filename (in path) to be used in file array later
        }
    
        // assemble file data (should be built like $_FILES since wp_handle_sideload() will be using)
        $file_array[\'tmp_name\'] = $tmp;                                                         // full server path to temp file
    
        if ( !empty( $filename ) ) {
            $file_array[\'name\'] = $filename . "." . $url_type[\'ext\'];                           // user given filename for title, add original URL extension
        } else {
            $file_array[\'name\'] = $url_filename;                                                // just use original URL filename
        }
    
        // set additional wp_posts columns
        if ( empty( $post_data[\'post_title\'] ) ) {
            $post_data[\'post_title\'] = basename($url_filename, "." . $url_type[\'ext\']);         // just use the original filename (no extension)
        }
    
        // make sure gets tied to parent
        if ( empty( $post_data[\'post_parent\'] ) ) {
            $post_data[\'post_parent\'] = $post_id;
        }
    
        // required libraries for media_handle_sideload
        require_once(ABSPATH . \'wp-admin/includes/file.php\');
        require_once(ABSPATH . \'wp-admin/includes/media.php\');
        require_once(ABSPATH . \'wp-admin/includes/image.php\');
    
        // do the validation and storage stuff
        $att_id = media_handle_sideload( $file_array, $post_id, null, $post_data );             // $post_data can override the items saved to wp_posts table, like post_mime_type, guid, post_parent, post_title, post_content, post_status
    
        // If error storing permanently, unlink
        if ( is_wp_error($att_id) ) {
            @unlink($file_array[\'tmp_name\']);   // clean up
            return $att_id; // output wp_error
        }
    
        // set as post thumbnail if desired
        if ($thumb) {
            set_post_thumbnail($post_id, $att_id);
        }
    
        return $att_id;
    }
    

  • 结束

    相关推荐

    404从wp-Content/Uploads/获取图像时

    我在获取图像时获得404状态,http仍然包含该图像。图像显示在浏览器中,但404代码中断了一些应用程序。对wp内容/上载/的调用被重定向到。htaccess:<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\\.php$ - [L] RewriteRule (.*) /index.php?getfile=$1 [L] </IfModule>&