I noticed in your comment…
Return the uploaded image ID, so to use to update the ACF field
Here is two functions which may help you out…
gf_handle_attachments()
to handle converting gform submission image file into wordpress media attachment. Returning media (attachment) id.gf_upload_to_media_acf()
to handle gform submitted image or multiple images and update defined acf (image, file or gallery) field with media attachment(s).
File types
jpeg
,jpg
,png
, andgif
will only work with this as
far as i’ve tested. File type
image
andgallery
fields. The
does not create any previews.
$filename
parameter is not just the filename, it is the full file url via https
or http
. Basically which ever protocol the gform submits over.
Here is the gf_handle_attachments()
function which handles converting a single $filename
into a wordpress media attachment. Returning the attachment id.
// save image to media library and then save the image to acf field
function gf_handle_attachments($filename, $parent_post_id) {
// check the type of file
// we'll use this as the 'post_mime_type'
$file_type = wp_check_filetype( basename( $filename ), null );
// get the path to the upload directory
$wp_upload_dir = wp_upload_dir();
// prepare an array of attachment post data
$attachment = [
'guid' => $wp_upload_dir['url'] . "https://wordpress.stackexchange.com/" . basename( $filename ),
'post_mime_type' => $file_type['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
];
// insert the attachment
$attach_id = wp_insert_attachment($attachment, $filename, $parent_post_id);
// make sure that this file is included, as wp_generate_attachment_metadata() depends on it
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// generate the metadata for the attachment, and update the database record
$attach_data = wp_generate_attachment_metadata($attach_id,$filename);
wp_update_attachment_metadata($attach_id, $attach_data);
// delete file from gf uploads
//unlink($filename);
// return attachment id
return $attach_id;
}
This is the gf_upload_to_media_acf()
function which takes the submitted gfrom file upload or gfrom multi-file uploads entry $value
and adds them to the defined acf (image, file or gallery) $field
into defined $post_id
.
// upload gf uploads media to post acf field
function gf_upload_to_media_acf($value, $post_id, $field) {
// if entry value begins with json brackets (gfrom multi-file upload entry)
if(substr($value,0,2) === '["') {
// decode gfrom entry value into array
$filenames = json_decode(stripslashes($value));
// lets begin multiple attachment array
$attach_ids = [];
// for each filenames as filename
foreach ($filenames as $filename) {
// handle each filename to wordpress media attachment linked to post
$attach_ids[] = gf_handle_attachments($filename, $post_id);
}
// update acf (gallery) field with the attachment ids
update_field($field, $attach_ids, $post_id);
// else if single gform image upload entry
} else {
// handle single media attachment and return attachment id
$attach_id = gf_handle_attachments($value, $post_id);
// update acf (image, file or gallery) field for post with the attachment id
update_field($field, $attach_id, $post_id);
}
}
Here is example usage using gform_confirmation
filter which creates a new post and then uses update_entry_data_fields()
function passing entry data which then handles specified entry fields (file uploads)…
// filter to fire when gfrom id 1 hits confirmation
add_filter('gform_confirmation_1', 'form_confirmation', 10, 4 );
// form confirmation function for above gform filter
function form_confirmation($confirmation, $form, $entry, $ajax) {
// new post settings array
$post = [
'post_type' => 'post',
'post_status' => 'pending'
];
// create new post for our submission using post array settings and return the newly created post id to result variable
$result = wp_insert_post($post);
// if result post id and is not a wp_error then...
if($result && !is_wp_error($result)) {
// get the created result post id
$post_id = $result;
// create our post title
$title="Post #" . $post_id;
// new submission array to use to update the created post
$submission = [
'ID' => $post_id,
'post_title' => $title,
'post_name' => sanitize_title($title)
];
// update the created submission post with new submission array data
wp_update_post($submission);
// update this post acf fields with entry data
update_entry_data_fields($post_id, $entry);
// return submission post id if you wana
return $post_id;
}
// return false
return false;
}
This update_entry_data_fields()
function is where we use gf_upload_to_media_acf()
function for gform submission file upload(s) entry data…
// update acf fields or anything else for defined post id using gform submission entry array data
function update_entry_data_fields($post_id, $entry) {
// if form submission entry is array
if(is_array($entry)) {
// for each entry item
foreach ($entry as $key => $value) {
// if entry key is our gform file upload or multi-file upload entry field
if($key === '1.8') {
// if we have a entry value
if($value) {
// image, file or gallery acf field name
$acf_field = 'acf_field_name';
// handle the media attachment(s) creation and add attachment(s) to acf (image, file, or gallery) field via acf_field_name
gf_upload_to_media_acf($value, $post_id, $acf_field);
}
}
}
}
}