Using Gravity Forms Multi-File Uploads When Creating A New Post

Covered in this Tutorial

Saving Gravity Form File Uploads to the WordPress Media Library

One of my favorite features in Gravity Forms is the ability to create draft posts on your site from front-end form submissions. This is an extremely powerful feature and can be used to populate almost any type of custom content you want to create for your site. (User-Generated Content baby!)

However, the process become a little “broken” when working with file upload fields.

When a user uploads files via a Gravity Form, the files aren’t stored in the WordPress media library, they are uploaded to a separated folder within your site’s uploads directory. When you think about it, this makes perfect sense. You wouldn’t typically want these files cluttering up your media library.

However, when using the form to create a draft post, I absolutely DO want the files to be stored in the media library.

Leverage the GP Media Library Plugin to Upload to Media Library

The best approach for this is a simple one, use the GP Media Library plugin from Gravity Wiz. When you activate, along with the core Gravity Perks plugin, you’ll see a new tab when adding any file upload type field.

Attaching the Uploaded Media to a Post Created from the Submitted Gravity Form

The above Perks plugin will take care of getting the uploaded files in your WordPress Media Library, however if your form is being used to create a new post, the files won’t be attached to the newly created post.

To do this, we’ll use the “gform_after_create_post” hook to grab all the newly created media files and updating their “post_parent” ID to be that of the newly created post from the form submission. To get the newly created file IDs, look at line #16 below:


$file_ids = gp_media_library()->get_file_ids( rgar( $lead, 'id' ), 2 );

The “2” at the end of that line should correspond to the field ID for the file upload field in your form.

TIP: You’ll notice in the snippet above that I used “rgar()” to grab the “id” from the $lead array. To help “future proof” your custom code that is built on top of other plugins, it is always best to use the utility functions provided by the plugin for interacting with their content.

Connecting Gravity Forms Multi-File Upload Fields to ACF Gallery Field

*UPDATE: GP Media Library v1.0.3 adds support for ACF gallery fields out of the box!

The Advanced Custom Fields Gallery Field is an extremely common way for managing a gallery of images on a post.

To connect the multi-file upload field to the ACF Gallery, we need to start by adding a standard File Upload field to our form. This DOES NOT have to be a Posts Field Custom Field, we’ll handle the connection to the newly created post in our snippet below.

The majority of this snippet is similar to the one above, we’re using the “gform_after_create_post” hook to grab all the newly created media files and then updating the gallery field meta_key (“gallery” in the example below) with those IDs.

Exit mobile version