I needed to have images uploaded by users through Gravity Forms be added to my WordPress media library. Googling for solutions led me to half a dozen plugins wanting $70 and up to solve this seemingly simply problem. Absolutely maddening.
For other developers pulling their hair out over this, here’s the quick and dirty code I wrote to add gravity forms file uploads to the media library. Add this to your functions.php file and adjust to taste:
add_action( 'gform_after_submission', 'iss_gf_after_submission', 10, 2 ); function iss_gf_after_submission($entry, $form) { //Walk through the form fields and find any file upload fields foreach ($form['fields'] as $field) { if ($field->type == 'fileupload') { //See if an image was submitted with this entry if (isset($entry[$field->id])) { $fileurl = $entry[$field->id]; // The ID of the post this attachment is for. Use 0 for unattached. $parent_post_id = 0; // Check the type of file. We'll use this as the 'post_mime_type'. $filetype = wp_check_filetype( basename( $fileurl ), null ); // Get the path to the upload directory. $wp_upload_dir = wp_upload_dir(); //Gravity forms often uses its own upload folder, so we're going to grab whatever location that is $parts = explode('uploads/', $entry[$field->id]); $filepath = $wp_upload_dir['basedir'].'/'.$parts[1]; $fileurl = $wp_upload_dir['baseurl']. '/'.$parts[1]; // Prepare an array of post data for the attachment. $attachment = array( 'guid' => $fileurl, 'post_mime_type' => $filetype['type'], 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $fileurl) ), 'post_content' => '', 'post_status' => 'inherit' ); // Insert the attachment. $attach_id = wp_insert_attachment( $attachment, $filepath, $parent_post_id ); //Image manipulations are usually an admin side function. Since Gravity Forms is a front of house solution, we need to include the image manipulations here. require_once( ABSPATH . 'wp-admin/includes/image.php' ); // Generate the metadata for the attachment, and update the database record. if ($attach_data = wp_generate_attachment_metadata($attach_id, $filepath)) { wp_update_attachment_metadata($attach_id, $attach_data); } else { echo ' <div id="message" class="error"> <h1>Failed to create Meta-Data</h1> </div> '; } wp_update_attachment_metadata( $attach_id, $attach_data ); } } } }
Hope that saves you some time, dev friends!
Was this resource helpful to you? Buy me a coffee so I can keep producing useful tools like this one! π
I think this is exactly what I’ve been looking for! But quick question about other media types… I need to not only allow images (png, jpg, tiff), but also pdfs and docx. Would those be added separately or if I already have them indicated under the file types allowed for the file upload in the form would that suffice?
I *believe* allowing it in the gravity form will do what you want. π
I have tried but not working for me. will you be able to help me, i have to store thumbnail id to post meta to use for another plugin and theme to display images on frontend. without storing it as thumbnail id, it wont display the images to frontend. I have copied it to function.php but dont know why its not executed after form submission. Do i need to set any css class fro field i am using to upload files. Please advise,
No, you shouldn’t have to set a CSS class for the above to work.
If you’d like to hire me for an hour of consulting time, please contact me through the contact form. π I’d be happy to check out your site. https://infinitesynergysolutions.com/contact-us/
Thank you so much for this Kim, this works for me however I get (no title) in the media library item list so is there a way to grab the title of the file and have it display that instead?
(Ultimately I’m using Posts Table Pro to show a list of the uploaded pdfs and need to display a clickable direct link to the file, you’ve helped me add the files to the media library but still need to work out how to display the direct url link in the posts table)
Lisa, were you able to get the file title to add to the media library? I am running into the same issue.
My apologies, you two! I had an error in my example code. I’ve updated the post to fix it.
Please change this line:
‘post_title’ => preg_replace( ‘/\.[^.]+$/’, ”, basename( $filename ) ),
to this:
‘post_title’ => preg_replace( ‘/\.[^.]+$/’, ”, basename( $fileurl ) ),
Hi Kim, thank you for that! One more question, have you tried this for the multi-file upload functionality of the file upload field?
I actually haven’t tested that out. If you’re going to, let me know how it goes?
Thanks for this!
Multi-upload works with this too! Thanks so much for making this available!
That is thrilling to hear! Thanks for reporting back! π
Hmm.. I have tried several methods now, including your code and it couldn’t get it to work. When I upload an image through Gravity it doesn’t show up in the media library
thank you for your time creating this code. but the images that I upload through multi file upload field are not added to the media library. Only the single file upload.