画像アップロードのカスタムメタボックスに少し問題があります。アップロードされた画像は添付ファイルとして作成されますが、wp_handle_uploadは投稿メタを更新するのではなく、「指定されたファイルのアップロードテストに失敗しました」というエラーを取り消すようです。添付ファイルは問題なく作成されているのに奇妙に思える?
コードは次のとおりです。
// Make the forms multipart
add_action('post_edit_form_tag', 'post_edit_form_tag');
function post_edit_form_tag() {
echo ' enctype="multipart/form-data"';
}
/* Setup custom meta boxes for posts */
$new_meta_boxes =
array(
"xxxx_kw1" => array(
"name" => "xxxx_kw1",
"std" => "",
"title" => "Keyword 1",
"type" => "text"
),
"xxxx_url1" => array(
"name" => "xxxx_url1",
"std" => "",
"title" => "URL 1",
"type" => "text"
),
"xxxx_kw2" => array(
"name" => "xxxx_kw2",
"std" => "",
"title" => "Keyword 2",
"type" => "text"
),
"xxxx_url2" => array(
"name" => "xxxx_url2",
"std" => "",
"title" => "URL 2",
"type" => "text"
),
"xxxx_kw3" => array(
"name" => "xxxx_kw3",
"std" => "",
"title" => "Keyword 3",
"type" => "text"
),
"xxxx_url3" => array(
"name" => "xxxx_url3",
"std" => "",
"title" => "URL 3",
"type" => "text"
),
"xxxx_image" => array(
"name" => "xxxx_image",
"std" => "",
"title" => "Image Upload",
"type" => "file"
)
);
function new_meta_boxes() {
global $post, $new_meta_boxes;
foreach($new_meta_boxes as $meta_box) {
$meta_box_value = get_post_meta($post->ID, $meta_box['name'], true);
if($meta_box['type'] == 'file') { $meta_box_value = get_post_meta($post->ID,'xxxx_attached_image', true); }
if($meta_box_value == "")
$meta_box_value = $meta_box['std'];
echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
echo'<p><label for="'.$meta_box['name'].'">'.$meta_box['title'].'</label></p>';
echo'<input type="'.$meta_box['type'].'" name="'.$meta_box['name'].'" value="'.$meta_box_value.'" size="55" />';
}
}
function create_meta_box() {
global $theme_name;
if ( function_exists('add_meta_box') ) {
add_meta_box( 'new-meta-boxes', 'PR xxxx Paid Information', 'new_meta_boxes', 'post', 'normal', 'high' );
}
}
function save_postdata( $post_id ) {
global $post, $new_meta_boxes;
$_POST = array_map('mysql_real_escape_string', $_POST);
foreach($new_meta_boxes as $meta_box) {
// Verify
if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
return $post_id;
}
if ( !current_user_can( 'edit_post', $post_id ))
return $post_id;
if($meta_box['type'] == 'file') : // if data is a file we'll need to treat if differently...
$data = $_FILES[$meta_box['name']];
// If the upload field has a file in it
if(isset($data) && ($data['size'] > 0)) {
// Get the type of the uploaded file. This is returned as "type/extension"
$arr_file_type = wp_check_filetype(basename($data['name']));
$uploaded_file_type = $arr_file_type['type'];
// Set an array containing a list of acceptable formats
$allowed_file_types = array('image/jpg','image/jpeg','image/gif','image/png');
// If the uploaded file is the right format
if(in_array($uploaded_file_type, $allowed_file_types)) {
// Options array for the wp_handle_upload function. 'test_upload' => false
$upload_overrides = array( 'test_form' => false );
// Handle the upload using WP's wp_handle_upload function. Takes the posted file and an options array
$uploaded_file = wp_handle_upload($data, $upload_overrides);
// If the wp_handle_upload call returned a local path for the image
if(isset($uploaded_file['file'])) {
// The wp_insert_attachment function needs the literal system path, which was passed back from wp_handle_upload
$location = $uploaded_file['file'];
// Generate a title for the image that'll be used in the media library
$file_title = $uploaded_file['file'];
// Set up options array to add this file as an attachment
$attachment = array(
'post_mime_type' => $uploaded_file_type,
'post_title' => preg_replace('/\.[^.]+$/', '', basename($file_title)),
'post_content' => '',
'post_status' => 'inherit',
'post_parent' => $post_id,
'guid' => $uploaded_file['url']
);
// Run the wp_insert_attachment function. This adds the file to the media library and generates the thumbnails. If you wanted to attach this image to a post, you could pass the post id as a third param and it'd magically happen.
$attach_id = wp_insert_attachment( $attachment, $location, $post_id );
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $location );
wp_update_attachment_metadata($attach_id, $attach_data);
// Before we update the post meta, trash any previously uploaded image for this post.
// You might not want this behavior, depending on how you're using the uploaded images.
$existing_uploaded_image = (int) get_post_meta($post_id,'xxxx_attached_image', true);
if(is_numeric($existing_uploaded_image)) {
wp_delete_attachment($existing_uploaded_image);
}
// Now, update the post meta to associate the new image with the post
update_post_meta($post_id,'xxxx_attached_image',$attach_id);
// Set the feedback flag to false, since the upload was successful
$upload_feedback = false;
} else { // wp_handle_upload returned some kind of error. the return does contain error details, so you can use it here if you want.
$upload_feedback = 'There was a problem with your upload: '.$uploaded_file['error'];
update_post_meta($post_id,'xxxx_attached_image',$attach_id);
}
} else { // wrong file type
$upload_feedback = 'Please upload only image files (jpg, gif or png).';
update_post_meta($post_id,'xxxx_attached_image',$attach_id);
}
} else { // No file was passed
$upload_feedback = false;
}
// Update the post meta with any feedback
update_post_meta($post_id,'xxxx_attached_image_upload_feedback',$upload_feedback);
else : // valid the input as normal
$data = $_POST[$meta_box['name']];
if(get_post_meta($post_id, $meta_box['name']) == "")
add_post_meta($post_id, $meta_box['name'], $data, true);
elseif($data != get_post_meta($post_id, $meta_box['name'], true))
update_post_meta($post_id, $meta_box['name'], $data);
elseif($data == "")
delete_post_meta($post_id, $meta_box['name'], get_post_meta($post_id, $meta_box['name'], true));
endif;
}
}
add_action('admin_menu', 'create_meta_box');
add_action('save_post', 'save_postdata');
誰かが手伝ってくれるなら、私は仮想親指とビールを送りたいです! :-)
乾杯、
デイブ
代わりに media_handle_upload()
を使ってみてください。私にも同じような問題を解決しました。