Home › Forums › General Issues › Populate ACF File field from Gravity Forms
As the title suggests, I’m trying to populate an ACF File field from a Gravity Forms upload field.
Can this be done? Where would one start?
Currently, I have tried this – https://rob.kinsella.dev/gravity-forms-assign-a-file-upload-to-an-advanced-custom-fields-file-field-wordpress-code-snippet/
/**
* Gravity Forms - handle file field upload and assign to ACF file field
*/
add_action( 'gform_advancedpostcreation_post_after_creation_2', function( $post_id, $feed, $entry, $form ) {
$file_field_entry_key = 5;
if ( $image_url = rgar( $entry, $file_field_entry_key ) ) {
$image_path = $_SERVER['DOCUMENT_ROOT'] . wp_make_link_relative( $image_url );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$filetype = wp_check_filetype( basename( $image_path ), null );
$file_array = [
'name' => basename( $image_path ),
'type' => $filetype,
'tmp_name' => $image_path,
'error' => 0,
'size' => filesize( $image_path ),
];
$attachment_id = media_handle_sideload( $file_array, $post_id );
update_field( 'YOUR_FIELD_NAME', $attachment_id, $post_id );
}
}, 10, 4 );
The issue with the code that you posted is that is says
update_field( 'YOUR_FIELD_NAME', $attachment_id, $post_id );
If you use the field name as this code suggests, and the field does not already have a value (exists in the database) then the ACF field will not be updated properly. When updating fields that do not already have values you must use the field key instead of the field name.
This is covered in the update_field() documentation https://www.advancedcustomfields.com/resources/acf-update_field/
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.