my code
function.php
function my_handle_attachment($file_handler,$post_id,$set_thu=false) {
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attach_id = media_handle_upload( $file_handler, $post_id );
if ( is_numeric( $attach_id ) ) {
update_post_meta( $post_id, '_product_image_gallery', $attach_id );
}
return $attach_id;
}
Front-end
if ( $_FILES ) {
$files = $_FILES["my_file_upload"];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array ("my_file_upload" => $file);
$newupload = my_handle_attachment( "my_file_upload", $post_id);
}
}
}
input
<input type="file" name="my_file_upload[]" multiple="multiple" >
I am using these codes so that it will upload all images and attach them to the correct post. It's not working only the last image will show up in products gallery image section. What is the error which I am unware of?