wp_generate_attachment_metadata返回一个空数组

时间:2014-01-08 22:41:31

标签: php wordpress upload

我正在写一个Wordpress插件,可以在我的博客上下载远程图像。

我创建了一个在本地上传远程图像,然后返回其ID的函数。 一切似乎都没问题

    $attach_data = wp_generate_attachment_metadata( $attach_id, $local_file );

返回一个空数组 - 它不应该。

除其他外,

wp_generate_attachment_metadata负责生成上传图像的缩略图。但是当我运行代码时,我没有创建缩略图。

我检查了我发送给函数的值,看起来是正确的:我有一个ID和上传文件的绝对路径,如codex中所述。 不过,我无法让我的代码正常工作:

$ attach_data 不应为空...

有人可以帮忙吗?

function upload_from_remote_url($url,$post_id){

    $url = $this->validate_remote_media_url($url); //check file is not on local server
    if (!$url) return false;

    if ($existing_id = $this->media_already_exists($url)) return $existing_id; //url already has been downloaded

    $upload_dir = wp_upload_dir();
    $wp_mime_types = wp_get_mime_types();

    //fetch image
    $response = wp_remote_get( $url );

    //get filename without extension
    $filename = basename( $url ); //get filename & extension
    $filename_strip = preg_replace('/\.[^.]*$/', '', $filename); //strip extension

    //get extension from content type,
    //because wp_upload_bits needs an extension and certain url don't have one.

    $file_type = wp_remote_retrieve_header( $response, 'content-type' );
    $extensions = array_search($file_type,$wp_mime_types);
    $extensions_arr = explode('|',$extensions);
    $extension = $extensions_arr[0];

    $new_filename = $filename_strip.'.'.$extension; //full name
    $new_filename = wp_unique_filename($upload_dir['path'], $new_filename); // be sure this name do not exist already

    $uploaded = wp_upload_bits($new_filename, '', wp_remote_retrieve_body( $response ) );
    if ($uploaded['error']) return false;

    $local_file = $uploaded['file'];
    $local_filename = basename($local_file);
    $local_filetype = wp_check_filetype( $local_filename, null );

    //Attachment options
    $attachment = array(
        'post_title'=> $local_filename,
        'post_mime_type' => $local_filetype,
        'post_status' => 'inherit'
    );

    // Add the image to your media library
    $attach_id = wp_insert_attachment( $attachment, $local_file, $post_id );

    if (!$attach_id) return false;

    $attach_data = wp_generate_attachment_metadata( $attach_id, $local_file );

    wp_update_attachment_metadata( $attach_id, $attach_data );

    //save source link so we do not import several times the same media
    update_post_meta($attach_id, 'grm_source', $url);

    return $attach_id;

}
顺便说一句,如果任何WP gourou对这段代码有什么要说的话......我会很乐意阅读它,因为关于上传文件的WP文档有点乱。我需要一些特定的东西,因为能够检索文件扩展名。我最终得到了这个,但也许你有更好的想法!

3 个答案:

答案 0 :(得分:1)

这就是最终为我解决的问题:

apply_filters('wp_handle_upload', array(
    'file' => $file_path, 
    'url' => $file_url, 
    'type' => $file_type), 
'upload');

说明:我不太清楚为什么这会为我修复错误,但我认为这或者与使用wp_handle_upload挂钩的插件有关或者过滤器将元数据添加到附件中,否则会在wp_generate_attachment_metadata函数中缺失。

全功能:

function add_to_media_lib($file_url, $file_path, $parent_post_id)
{
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');

// Check the type of tile. We'll use this as the 'post_mime_type'.
$file_type = wp_check_filetype(basename($file_url), null);

// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();

// Prepare an array of post data for the attachment.
$attachment = array(
    'guid' => $wp_upload_dir['url'] . '/' . basename($file_url),
    'post_mime_type' => $file_type['type'],
    'post_title' => preg_replace('/\.[^.]+$/', '', basename($file_url)),
    'post_content' => '',
    'post_status' => 'inherit',
    'post_parent' => $parent_post_id
);

// Insert the attachment.
$attach_id = wp_insert_attachment($attachment, $file_url, $parent_post_id);

// apply filters (important in some environments)
apply_filters('wp_handle_upload', array('file' => $file_path, 'url' => $file_url, 'type' => $file_type), 'upload');


// Generate the metadata for the attachment, and update the database record.
if ($attach_data = wp_generate_attachment_metadata($attach_id, $file_path)) {
    wp_update_attachment_metadata($attach_id, $attach_data);
} else {
    echo '<div id="message" class="error"><h1>Failed to create PDF-thumbnail Meta-Data</h1><pre>' . print_r($attach_data) . '</pre></div>';
}

return $attach_id;
}

答案 1 :(得分:1)

我有一个类似的问题,其中缺少mime类型。由于我只使用一种mime类型,因此由

修复
'post_mime_type' => 'image/jpeg'

之后,它仍然没有更新元数据,而是用&#34; wp_update_attachment_metadata&#34;强制更新。解决了这个问题:

$attach_data = wp_generate_attachment_metadata($attach_id, $file_path);
wp_update_attachment_metadata($attach_id, $attach_data);

答案 2 :(得分:0)

我知道这个话题很旧,但我遇到了类似的问题,对我有用的是在“设置”>>“媒体”下启用将缩略图裁剪到精确尺寸(通常缩略图是成比例的)。顺便说一下,我正在使用 media_handle_sideload() 函数。