WordPress上传自定义文章的缩略图Meta
问题描述:
我为每篇文章设置了缩略图的自定义文章元字段。这个函数被调用的正确,并且一切正常,直到最后一行update_post_meta
WordPress上传自定义文章的缩略图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);
}
}