Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Advanced Custom Fields & Gravity Forms Image Upload WordPress


Hey all,

I'm using two plugins, Advanced Custom Fields and Gravity Forms.

The problem is that a form created with Gravity Form, would'nt write to ACF custom fields.
I found a solution on Wp-Questions, ( but it doesnt involve images.

I would like an image upload field to save content in a ACF image type custom field.

Any ideas?

Answers (2)


Jurre Hanema answers:

This one is a little bit tricky because you are required to enter the ID of your Gravity Forms upload field and the name of your ACF custom field in the code. Then you can try this code I just hacked together:

add_action('gform_post_submission', 'wpq_process_file', 10, 2);

function wpq_process_file($entry, $form)
global $wpdb;

// Modify these two variables as needed

$gf_upload_id = 4;
$acf_field_name = 'my_image';

// Bail if we are not creating a post


// Create attachment from image

$filename = $entry[$gf_upload_id];

$wp_filetype = wp_check_filetype(basename($filename), null);

$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit'

$attach_id = wp_insert_attachment($attachment, $filename, $entry['post_id']);

require_once(ABSPATH . 'wp-admin/includes/image.php');

$attach_data = wp_generate_attachment_metadata($attach_id, $filename);

wp_update_attachment_metadata($attach_id, $attach_data);

// Save attachment ID to ACF


$query = $wpdb->prepare("SELECT id FROM {$wpdb->prefix}acf_fields WHERE name = %s", $acf_field_name);
$acf_field = $wpdb->get_row($query);

update_post_meta((int)$entry['post_id'], $acf_field_name, $attach_id);

$query = $wpdb->prepare("SELECT meta_id FROM {$wpdb->prefix}postmeta ORDER BY meta_id DESC LIMIT 1");
$meta_id = $wpdb->get_var($query);

$query = $wpdb->prepare("INSERT INTO {$wpdb->prefix}acf_values (field_id, value, post_id) VALUES (%d, %d, %d)", $acf_field->id, $meta_id, $entry['post_id']);

You have got to modify the variables $gf_upload_id and $acf_field_name so that the one contains the ID of the GF upload field and the other contains the name of the ACF custom field.

You can easily find the ID of the GF upload field by going to the form admin and selecting the field with Firebug or any similar tool. The HTML will read something like this:

<li id="field_4" class="selectable gfield"> which case the ID you need is of course 4.

Good luck!

Alex Coutsogiannopoulos comments:

Seems like the only approach, i will try it and see how it goes.

Thank you.


Luis Cordova answers:

first of all if you are not on a clean install then you will need to get this in a clean install

because the ACF creates different db structure for when it is upgraded than when it is clean installed.

second image should be the same, do you have some code?

Alex Coutsogiannopoulos comments:

I know the differences between acf clean install and not, rest of the fields are working.

What do you mean "should be the same"?.
Also what kind of code do you need, cause everything is generated by GF.