@cwoody1980, i had a similar need to yours. Instead of using the plugin though, I used Gravity Form’s gform_after_submission
hook.
Basically the code below grabs the file and post id from the submission, uploads the file to the media library, then updates the ACF field using ACF’s update_field
function.
<?php
add_action('gform_after_submission', 'update_acf_file_from_gravity_form_with_attachment', 10, 2);
function update_acf_file_from_gravity_form_with_attachment($entry, $form) {
// check if it's the correct form
if ($form['id'] != 4) return;
// Specify the ID of the file upload field
$file_field_id = '21';
$file_url = $entry[$file_field_id];
// var_dump($entry);
// Specify post ID field
$post_id_field_id = '15';
$post_id = $entry[$post_id_field_id];
// Ensure a file URL and post ID are provided
if (!empty($file_url) && !empty($post_id)) {
// Proceed to upload the file to the WordPress Media Library
$attachment_id = upload_file_to_media_library($file_url, $post_id);
if ($attachment_id) {
var_dump($attachment_id);
var_dump($post_id);
// Use the ACF update_field function to update the file field with the attachment ID
$field_key = 'rental_pricing';
update_field($field_key, $attachment_id, $post_id);
}
}
}
function upload_file_to_media_library($file_url, $post_id) {
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');
// Download file to temp location
$tmp = download_url($file_url);
// Check for download errors
if (is_wp_error($tmp)) {
// Handle error
return false;
}
$file_array = array();
// Set variables for storage
// Extract filename and extension from the URL
$file_array['name'] = basename($file_url);
// Prepare an array for the attachment
$file_array['tmp_name'] = $tmp;
// Upload the file to the Media Library
$id = media_handle_sideload($file_array, $post_id);
// Check for handle sideload errors.
if (is_wp_error($id)) {
@unlink($file_array['tmp_name']); // Clean up
return false;
}
return $id; // Return the attachment ID
}