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 );```

Are you using Advanced Post Creation? The tutorial you linked was designed with APC in mind, so I thought I’d check. :slight_smile:

If so, you might check out this tutorial from Gravity Wiz: Uploading Files to Advanced Custom Fields Using the Gravity Forms Advanced Post Creation Add-On - Gravity Wiz

If you aren’t using APC, they have another tutorial that might be useful: Upload Files and Images from Gravity Forms to Advanced Custom Fields

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.