最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

woocommerce offtopic - How to add multiple product gallery images from front-end

programmeradmin0浏览0评论

-I am running a woo commencers shop

-Users have the ability to add products using wp insert post

-The product gallery multiple image upload will only add the last image to the post but in media they are all attached to the correct post

This is 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" >

This will upload all images and attach them to the correct post but only the last image will show up in Products gallery image section. What am i doing wrong?

-I am running a woo commencers shop

-Users have the ability to add products using wp insert post

-The product gallery multiple image upload will only add the last image to the post but in media they are all attached to the correct post

This is 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" >

This will upload all images and attach them to the correct post but only the last image will show up in Products gallery image section. What am i doing wrong?

Share Improve this question edited Aug 30, 2017 at 17:20 Tcmxc asked Aug 29, 2017 at 23:19 TcmxcTcmxc 2175 silver badges15 bronze badges 3
  • Did you get the solution for this issue? I have a similar type of issue. – Nilesh G Commented Feb 27, 2019 at 3:52
  • @NileshG yes I did check my answer – Tcmxc Commented Feb 27, 2019 at 19:04
  • Please help with thios similar problem: wordpress.stackexchange/questions/359829/… – Mariusz Commented Mar 2, 2020 at 8:59
Add a comment  | 

2 Answers 2

Reset to default 0
if ( ! empty( $_FILES['muti_files'] )  ) {
            $files = $_FILES['muti_files'];
            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("muti_files" => $file);
                $i=1;
                    foreach ($_FILES as $file => $array) {
                          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);
                            $vv .= $attachment_id . ",";
                            $i++;
                    }
                    update_post_meta($post_id, '_product_image_gallery',  $vv);
            }
        }

More optimized!

if ( ! empty( $_FILES['muti_files'] )  ) {
        $files = $_FILES['muti_files'];
        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("muti_files" => $file);
            $i=1;

require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');

                foreach ($_FILES as $file => $array) {
                      if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) __return_false();

                        $attachment_id = media_handle_upload($file, $post_id);
                        $vv .= $attachment_id . ",";
                        $i++;
                }
                update_post_meta($post_id, '_product_image_gallery',  $vv);
        }
    }
发布评论

评论列表(0)

  1. 暂无评论