Gravity Forms multiple multi-file upload fields to automatically added to the WordPress Media Library

1.6k Views Asked by At

I have a Gravity Form that has multiple pages and on at least 3 of the pages there is a multi-file upload field. I am trying to take the uploaded items from each of these fields and automatically add them to the media library for further manipulation. Using this solution I am able to take the files from the first page and upload them to the media library, however, I cannot figure out how to get the files from the other two pages.

Below is my code snippet. If I adjust the $submit = $entry["4"]; The field ID of the multi upload to include the additional upload fields, it breaks my foreach() statement saying an invalid argument is supplied.

I've also tried adding additional $submit sections to include the other field ids, but it only takes the last field id entry. Adding this whole section of code multiple times also does not work since it is the same form, just a different pages, and I get:

Fatal error: Cannot redeclare copy_post_image2() (previously declared in ...\functions.php

//Multi-file upload code: upload multiple files through Gravity Forms and have them automatically moved to the WordPress Media Library
add_filter("gform_after_submission_1", "my_add_post_attachments",10,2); //Your form ID
function my_add_post_attachments($entry, $form){


$submit = array();
$submit = $entry["4"]; //The field ID of the multi upload
$submit = stripslashes($submit);
$submit = json_decode($submit, true);

foreach ($submit as $key => $value){

require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');
$post_id = 200; //The post ID of the page you are adding images
$url = $value;

$file = copy_post_image2($url, $post_id);

if(!$file)
return false;

$name = basename($url);
$name_parts = pathinfo($name);
$name = trim( substr( $name, 0, -(1 + strlen($name_parts['extension'])) ) );

$url = $file['url'];
$type = $file['type'];
$file = $file['file'];
$title = $name;
$content = '';

$attachment = array(
'post_mime_type' => $type,
'guid' => $url,
'post_parent' => $post_id,
'post_title' => $title,
'post_content' => $content,
);

$id = wp_insert_attachment($attachment, $file, $post_id);
if ( !is_wp_error($id) ) {
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
}
}
}

function copy_post_image2($url, $post_id){
$time = current_time('mysql');

if ( $post = get_post($post_id) ) {
if ( substr( $post->post_date, 0, 4 ) > 0 )
$time = $post->post_date;
}

//making sure there is a valid upload folder
if ( ! ( ( $uploads = wp_upload_dir($time) ) && false === $uploads['error'] ) )
return false;

$name = basename($url);

$filename = wp_unique_filename($uploads['path'], $name);

// Move the file to the uploads dir
$new_file = $uploads['path'] . "/$filename";

$uploaddir = wp_upload_dir();
$path = str_replace($uploaddir["baseurl"], $uploaddir["basedir"], $url);

if(!copy($path, $new_file))
return false;

// Set correct file permissions
$stat = stat( dirname( $new_file ));
$perms = $stat['mode'] & 0000666;
@ chmod( $new_file, $perms );

// Compute the URL
$url = $uploads['url'] . "/$filename";

if ( is_multisite() )
delete_transient( 'dirsize_cache' );

$type = wp_check_filetype($new_file);
return array("file" => $new_file, "url" => $url, "type" => $type["type"]);

}

Any suggestions on how to add the additional multi-file upload fields would be greatly appreciated.

1

There are 1 best solutions below

0
On BEST ANSWER

Based on the error, you're probably declaring that copy_post_image2 function twice. I don't see it listed twice in the code you shared but if you search for it in your functions.php file, I bet you'll find a second copy of it.

As far as the end goal of copying all images from all file upload fields on all pages, check out Gravity Forms Media Library. It'll handle this like magic and supports displaying the image at different sizes via merge tags.