2

I am trying to add WooCommerce Product Gallery using Gravity forms multiple file upload but product gallery should not be added

add_action( 'gform_after_submission_3', 'set_post_content', 10, 2 );
function set_post_content( $entry, $form ) {

    if (!function_exists('wp_generate_attachment_metadata')) {
        require_once(ABSPATH . 'wp-admin/includes/image.php');
      }

    $uploaded_files = json_decode(rgpost("gform_uploaded_files"));
    
    foreach($uploaded_files->input_76 as $key => $data){
    if (isset($_FILES[$data])) {
            $file_url = $data->input_76; //great but what is its url?
            $upload_dir = wp_upload_dir(); //where do you want to put it?
            $file_data = file_get_contents($file_url); //show me what you're made of
            $filename = basename($file_url); //so cute but what's its name?
            if (wp_mkdir_p($upload_dir['path'])) //can we put it there?
                $file = $upload_dir['path'] . '/' . $filename; //yes great
                  else //or no, okay fine let's try somewhere else
                $file = $upload_dir['basedir'] . '/' . $filename; //get the whole location
            file_put_contents($file, $file_data); // tada home at last
            //$wp_filetype = wp_check_filetype($filename, array('pdf' => 'application/pdf','pdf' => 'application/x-pdf') ); //is it the right type of of file?
            $attachment = array(//set up the attachment
                //'post_mime_type' => $wp_filetype['type'],
                'post_title' => sanitize_file_name($filename),
                'post_content' => '',
                'post_status' => 'inherit'
            );
             
            $attach_id = wp_insert_attachment($attachment, $file, $entry['post_id']); //insert attachment
            $attach_data = wp_generate_attachment_metadata($attach_id, $file); //asign the meta
            wp_update_attachment_metadata($attach_id, $attach_data); //update the post
            update_post_meta($entry['post_id'], '_product_image_gallery', $attach_id);
        }
    }
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

0 Answers0