Using the Gravity Forms + Custom Post Types WordPress plugin to map your Gravity-Forms-generated posts to a custom post type is very handy, but I noticed that it doesn’t seem to work with file uploads. This is because by default, Gravity Forms saves files to /wp-content/uploads/gravity_forms and stores their URLs. ACF on the other hand, saves files to the WP Media Library and stores their attachment IDs. The class below can be used to solve problem this by doing the following:

  • After the form has been submitted, get the file from the GF uploads directory and insert it into the WordPress Media Library.
  • Update the post to save the attachment ID in the ACF form field, so ACF knows where to look for it.
  • Delete the original file that was uploaded to the GF uploads directory.

Be sure to change line 53 to reflect the ID of your Gravity Form, and lines 62 & 63 to reflect the Gravity Forms an ACF field IDs that you want to map to one another.

13 comments

  1. Fabio infante December 16, 2016 at 8:43 pm

    Reply

    Hi kellen, thanks for share, I want to upload multiple files for several custom fields at the same time on the same form

  2. Tiffany Israel April 4, 2017 at 11:07 am

    Reply

    Is there a way to get this to work with out a post submission? Also for line 48 is this just the title of the Gravity forms, no special “nicename” or anything?

  3. Aaron June 21, 2017 at 6:15 pm

    Reply

    Thanks for posting this. I have been unable to get it to work. Is it simply including the file into functions.php and updating the three variables in the file?

    Thanks!

    • Kellen Mace July 17, 2017 at 7:05 am

      Reply

      Hey Aaron – as this was originally, written, you’d have to include the code in your project, then instantiate the class using new WDSP_Event_Form();. I’ve updated it so that it gets instantiated on the last line though, so you don’t have to worry about that anymore. Although it would be possible to paste it all into functions.php, I would recommend creating a new plugin with a header at the top (an example of one is here: https://developer.wordpress.org/plugins/the-basics/header-requirements/), then pasting all the code from this post into it, changing the new lines I mention in the post. Then you would be able to save that plugin to your web server, activate the plugin and be all set. Best of luck!

  4. Guy Lev July 11, 2017 at 12:28 pm

    Reply

    Hi kellen.
    For some reason this doesn’t work for me 🙁
    I cannot see the image in the media library and it doesn’t show in the gallery 🙁
    Can you help somehow?

    Thanks!!!!!!!
    Guy

    This is my function.php

    https://gist.github.com/oneguy9/aa86e6d54de1e6430efa4b64ed84b101

    • Kellen Mace July 17, 2017 at 7:45 am

      Reply

      Hey Guy – as this was originally, written, you’d have to include the code in your project, then instantiate the class using new WDSP_Event_Form();. I’ve updated it so that it gets instantiated on the last line though, so you don’t have to worry about that anymore. Although it would be possible to paste it all into functions.php, I would recommend creating a new plugin with a header at the top (an example of one is here: https://developer.wordpress.org/plugins/the-basics/header-requirements/), then pasting all the code from this post into it and changing the new lines I mention in the post. Then you would be able to save that plugin to your web server, activate the plugin and be all set. Best of luck!

  5. Joshua August 18, 2017 at 10:51 pm

    Reply

    Hi Kellen,

    Thanks for this.

    Using your code, the image is uploaded to the Media Library but I’m saving the image to an ACF field in a user’s profile.

    Do I need to change lines 58, 64, 87 to the user id?

    Best,

    Joshua

  6. Eivind Nordengen August 25, 2017 at 6:35 am

    Reply

    Hey! Code works perfectly (and is so far the only working solution I’ve found for this problem). But is there a way to implement more than one field and more than one form in this code? Tried to just copy the whole thing as many times as needed (in functions.php), but that gave me an error (and it also seems a bit excessive to have to copy the whole code many times). Is there an easy way to have more fields linked?

  7. Valerio Cassimatis February 2, 2018 at 1:32 am

    Reply

    Well done. Works and is well written.

    Only thing I’d suggest is setting the values that need to be changed at the top of the file as constants.

  8. Abe February 21, 2018 at 1:23 am

    Reply

    KELLEN – you saved the night and the day – this code works just fine and awesome 🙂

    I made a plugin with a header off of the page you suggested – it took me half an hour to fin the field key for ACF from the dropdown LOL rest was a cakewalk it WORKS!!!

    Awesome …

  9. Abe March 1, 2018 at 10:45 pm

    Reply

    Hi again, Kellen –

    If I wanted to use this code to populate the file upload URL field for a custom post type, called, ‘downloads’ (created by default via the front end submissions plugin by EDD), Im wondering what changes may be needed apart from the ones I already made!?

    In my test, the Gravity form ID and upload field to be mapped are easy enough. Im getting the file moved into the WP gallery.

    However, Ive used a couple different IDs to replace the ACF ID field, but to no avail so far.

    One of the IDs for ACF I substituted was 1. Please see image why.

    Any suggestions that you may have? Im thinking it should be a small fix – since the problem is half resolved.

    http://jmp.sh/LImeoYQ

  10. Abe March 1, 2018 at 11:15 pm

    Reply

    Also, just noticed in the database that a meta value actually HAS come in.

    In the table wp-postmeta …

    _wp_attached_file has the value of 2018/03/ItIsNotTooLate.pdf (which was ofcourse the file uploaded via Gravity forms )

    Therefore there is some glue work happening already – its just not mapped into the “Prices and Files” field. As seen underneath.

    http://jmp.sh/jJbUcYc

    Also, normal creation of a download post type from the backend reveals that the file upload should stick to the meta key: edd_download_files, with the meta value (for the same file as above, taken from the media gallery now that its available) as

    a:1:{i:1;a:6:{s:5:”index”;s:1:”0″;s:13:”attachment_id”;s:3:”351″;s:14:”thumbnail_size”;s:5:”false”;s:4:”name”;s:14:”ItIsNotTooLate”;s:4:”file”;s:79:”http://localhost:8888/wordpress-2/wp-content/uploads/2018/03/ItIsNotTooLate.pdf”;s:9:”condition”;s:3:”all”;}}

    Also in this case, I dont see the meta key _wp_attached_file which appears to come only when we use the front end form.

Leave a reply

Your email address will not be published. Required fields are marked *