2014-01-08 78 views
4

我正在寫一個Wordpress插件來在我的博客上下載遠程圖像。wp_generate_attachment_metadata返回一個空的數組

我做了一個函數,在本地上傳一個遠程鏡像,然後返回它的ID。 一切似乎都OK,只是

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

返回我一個空數組 - 它不應該。

wp_generate_attachment_metadata等等,負責生成上傳圖像的縮略圖。但是當我運行我的代碼時,我沒有創建縮略圖。

我檢查了我發送給函數的值,它們似乎是正確的:我有一個ID和上載文件的絕對路徑,如代碼中所述。 不過,我不能管理有我的代碼工作:

$ 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狗肉湯有什麼話要說這段代碼...我會很樂意閱讀它,因爲有關上傳文件的WP文檔是有點亂。我需要一些特定的東西,因爲能夠檢索文件擴展名。我結束了這個,但也許你有一些更好的想法!

回答

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; 
} 
+0

這是否也爲PDF創建縮略圖,對我來說,我的代碼是爲圖像創建縮略圖,但不是用於pdf或rar或zip文件? –

0

我已經在那裏的MIME類型是缺少一個類似的問題。由於我只使用一個MIME類型,它是固定的

'post_mime_type' => 'image/jpeg' 

之後,它仍然沒有更新元數據,但強制更新「wp_update_attachment_metadata」解決了這個問題:

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