2011-03-08 76 views
0

我爲每篇文章設置了縮略圖的自定義文章元字段。這個函數被調用的正確,並且一切正常,直到最後一行update_post_metaWordPress上傳自定義文章的縮略圖Meta

有趣的是,我可以回顯出$imageURL並獲取正確的地址,並且文件上傳正常。我甚至可以使用update_post_meta以及任何其他值,無論它是字符串還是函數中的其他變量,但只要我嘗試使用$imageURL$uploaded_file['url'],它只是將後置元素設置爲空字符串。

我在3.1之前的WordPress開發的項目上使用了這個片段,但是這個是3.1。這可能與它有關嗎?我有點懷疑它,因爲這似乎是那些超級怪異的錯誤之一。

function tcr_save_thumbnail($post_id, $post) { 
    if (!wp_verify_nonce($_POST['eventmeta_noncename'], plugin_basename(__FILE__))) { 
     return $post->ID; 
    } 

    if (!current_user_can('edit_post', $post->ID)) 
     return $post->ID; 

    if(!empty($_FILES['tcr_thumbnail_meta']['name'])) { //New upload 
     require_once(ABSPATH . 'wp-admin/includes/file.php'); 
     $override['action'] = 'editpost'; 
     $uploaded_file = wp_handle_upload($_FILES['tcr_thumbnail_meta'], $override); 

     $post_id = $post->ID; 
     $attachment = array(
      'post_title' => $_FILES['tcr_thumbnail_meta']['name'], 
      'post_content' => '', 
      'post_type' => 'attachment', 
      'post_parent' => $post_id, 
      'post_mime_type' => $_FILES['tcr_thumbnail_meta']['type'], 
      'guid' => $uploaded_file['url'] 
     ); 
     // Save the data 
     $id = wp_insert_attachment($attachment,$_FILES['tcr_thumbnail_meta'][ 'file' ], $post_id); 
     wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $_FILES['tcr_thumbnail_meta']['file'])); 
     $imageURL = $uploaded_file['url']; 
     update_post_meta($post->ID, "tcr_thumbnail_meta", $imageURL); 
    } 
} 

回答