2013-06-25 3 views
1

Я создал код, который публикует продукт из front-end-проблемы, когда я загружаю галереи изображений из frontend, но только для загрузки последнего изображения.woocommerce product gallery images upload from front-end

здесь код

<form action="#" method="post" enctype="multipart/form-data"> <input type="file" name="agp_gallery[]" id="agp_image_files" style="width:90%; margin-left:5px;" multiple> 

здесь функция Multiupload

agp_process_image('agp_image_file', $post_id, $result['caption'], $result['content'], $result['author']); 
      if ($_FILES) { 
$files = $_FILES['agp_gallery']; 
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("agp_gallery" => $file); 

foreach ($_FILES as $file => $array) { 

    agp_process_wooimage($file, $post_id, $result['caption']); 
} 
} 
} 
} 

здесь главная функция

function agp_process_wooimage($file, $post_id, $caption){ 

    if ($_FILES[$file]['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'); 

    $attachment_id = media_handle_upload($file, $post_id); 

    update_post_meta($post_id, '_product_image_gallery', $attachment_id); 

    $attachment_data = array(
    'ID' => $attachment_id, 
    'post_excerpt' => $caption 
); 

    wp_update_post($attachment_data); 

    return $attachment_id; 

} 

ответ

1

вот окончательный код: -

// Get the upload attachment files 
    if ($_FILES) { 
$files = $_FILES['agp_gallery']; 
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("agp_gallery" => $file); 

      foreach ($_FILES as $file => $array) 
      { 
       $newupload = agp_process_wooimage($file,$post_id); 
      } 
     } 

    } 
} 

function agp_process_wooimage($file, $post_id){ 

    if ($_FILES[$file]['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'); 

    $attachment_id = media_handle_upload($file, $post_id); 
update_post_meta($post_id, array_push($post_id, '_product_image_gallery', $attachment_id)); 

    return $attachment_id; 

}