1
add_meta_boxを使用して、投稿に画像をアップロードするために8つのフィールドを作成しようとしましたが、何らかの理由で画像の一部が正しくアップロードされません幅と高さも正しいようにしてください。複数の画像アップローダーをWordPressのメタボックスに追加
フィールド:
function facilitiesimage_box_render(){
$status_message = get_post_meta($post->ID, 'facilities_img_error', true);
if($status_message) {
echo '<div class="error" id="message"><p>' . $status_message . '</p></div>';
}
$max_no_img=8; // Maximum number of images value to be set here
echo '<input type="hidden" name="MAX_FILE_SIZE" value="1524288">';
echo "<table border='0' width='400' cellspacing='0' cellpadding='0' align=center>";
for($i=1; $i<=$max_no_img; $i++){
echo "<tr><td>Images $i</td><td><input type=file name='images[]' class='bginput'></td></tr>";
}
echo "<tr><td colspan=2 align=center><input type=submit value='Add Image'></td></tr>";
echo "</form> </table>";
}
コードの残りの部分:手の前に
function update_facilitiesimage($post_id, $post) {
while(list($key,$value) = each($_FILES['images']['name'])) {
if(!empty($value)){
$arr_file_type = wp_check_filetype(basename($value));
$uploaded_file_type = $arr_file_type['type'];
$allowed_file_types = array('image/jpg', 'image/jpeg', 'image/gif', 'image/png');
if(in_array($uploaded_file_type, $allowed_file_types)){
$upload_dir = wp_upload_dir();
$newname = strtolower(str_replace(' ', '-', $value));
$add = $upload_dir['path'] . '/' . $newname;
copy($_FILES['images']['tmp_name'][$key], $add);
list($width, $height) = getimagesize($add);
$upload_image_width = '630';
$upload_image_height = '350';
if($width < $upload_image_width || $height < $upload_image_height) {
$upload_feedback = 'Sorry, the image ' . $value . ' does not meet the minimum height/width requirements. Please upload another image.<br/>';
update_post_meta($post_id, 'facilities_img_error', $upload_feedback);
unlink($add);
}
else {
$upload_image_width = '630';
$upload_image_height = '350';
$resized = image_resize($add, $upload_image_width, $upload_image_height, true);
unlink($add);
$file_name_and_location = $add;
$file_title_for_media_library = 'your title here';
$attachment = array(
'post_mime_type' => $uploaded_file_type,
'post_title' => 'Uploaded image ' . addslashes($file_title_for_media_library),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $file_name_and_location);
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
$attach_data = wp_generate_attachment_metadata($attach_id, $file_name_and_location);
wp_update_attachment_metadata($attach_id, $attach_data);
$ra = 'attached_facility-img' . $key;
$existing_uploaded_image = (int) get_post_meta($post_id, $ra, true);
if(is_numeric($existing_uploaded_image)) {
wp_delete_attachment($existing_uploaded_image);
}
update_post_meta($post_id, $ra ,$attach_id);
$upload_feedback = false;
update_post_meta($post_id, 'facilities_img_error', $upload_feedback);
$fname = strrchr($resized, '/');
$img = $upload_dir['baseurl'] . $fname;
$table = 'facilitiesimage-' . $key;
delete_post_meta($post_id, $table);
update_post_meta($post_id, $table, $img);
}
}
else {
$upload_feedback = 'Please upload only image files (jpg, gif or png).<br/>';
update_post_meta($post_id, 'facilities_img_error', $upload_feedback);
}
}
}
}
add_action('save_post','update_facilitiesimage',1,2);
私はちょうどに私がどんな巨大なERROR(または恐怖)をお詫び申し上げますそう学んでいます私のコード。
これらの画像はすべて更新されず、これらの画像はJPEGであり、ファイルのサイズとサイズはコード内の値をホワイトニングしています。使用
:
echo "<pre>";
echo "POST:";
print_r($_POST);
echo "FILES:";
print_r($_FILES);
echo "</pre>";
を私は値が正しい(画像は「検証」を渡す)が、画像がアップロードされませんことがわかりました。スクリプトは他のJPEG画像をアップロードする際に問題がないことに注意してください。
任意のヘルプは、フィールドが、ここでOKです事前
Awsomeさん、おかげであなたのためです!それは素晴らしい作品 – user1068410