I am using Gravity forms which is uploading media and recreating the image into the relevant media folder over https.
However, on my server, this doesn't appear to be reading the Gravity File folder, or the contents of the image correctly. The files are being created however with no data from the image. (I have included my full upload code below. I found an article that "file_get_contents" had issues in regards to SSLs locally, and this appears to be working locally as intended.
Thoughts / guidance appreciated
(Note, Prefix is just an additional parameter, and obviously $imgArr is an array of image URLs that i am passing from the Gravity Form by form of
json_encode($entity[x])
name is from another variable from the Gravity form as an entity also.
Thankyou in advance.
function createImage($prefix, $imgArr, $name){
$i = 1;
//Sorting out the URLs prior to creation
foreach($imgArr as $url) {
$finalUrl = str_replace('""', '',$url);
$newName = $name . $prefix . '-'. $i;
$i++;
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$upload_dir = wp_upload_dir();
$image_data = file_get_contents($finalUrl, false, stream_context_create($arrContextOptions));
//Strip out the old filename and replace with the befores name
$slash = strrpos($finalUrl, '/', 0);
$slash++;
$fileName = array_pop(str_split($finalUrl, $slash));
$fileExt = array_pop(explode('.', $fileName));
$newFileName = $newName . '.' . $fileExt;
if(wp_mkdir_p($upload_dir['path']))
$file = $upload_dir['path'] . '/' . $newFileName;
else
$file = $upload_dir['basedir'] . '/' . $newFileName;
file_put_contents($file, $image_data);
$wp_filetype = wp_check_filetype($newFileName, null);
$upload_id = array(
'post_mime_type' => $wp_filetype['type'],
'post_title'=> sanitize_file_name($newFileName),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $upload_id, $file, $post_ID);
if (!is_wp_error($attach_id)) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attachment_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attachment_data );
set_post_thumbnail( $post_ID, $attachment_data );
}
//Return the uploaded image ID, so to use to update the ACF field
}
}
I am using Gravity forms which is uploading media and recreating the image into the relevant media folder over https.
However, on my server, this doesn't appear to be reading the Gravity File folder, or the contents of the image correctly. The files are being created however with no data from the image. (I have included my full upload code below. I found an article that "file_get_contents" had issues in regards to SSLs locally, and this appears to be working locally as intended.
Thoughts / guidance appreciated
(Note, Prefix is just an additional parameter, and obviously $imgArr is an array of image URLs that i am passing from the Gravity Form by form of
json_encode($entity[x])
name is from another variable from the Gravity form as an entity also.
Thankyou in advance.
function createImage($prefix, $imgArr, $name){
$i = 1;
//Sorting out the URLs prior to creation
foreach($imgArr as $url) {
$finalUrl = str_replace('""', '',$url);
$newName = $name . $prefix . '-'. $i;
$i++;
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$upload_dir = wp_upload_dir();
$image_data = file_get_contents($finalUrl, false, stream_context_create($arrContextOptions));
//Strip out the old filename and replace with the befores name
$slash = strrpos($finalUrl, '/', 0);
$slash++;
$fileName = array_pop(str_split($finalUrl, $slash));
$fileExt = array_pop(explode('.', $fileName));
$newFileName = $newName . '.' . $fileExt;
if(wp_mkdir_p($upload_dir['path']))
$file = $upload_dir['path'] . '/' . $newFileName;
else
$file = $upload_dir['basedir'] . '/' . $newFileName;
file_put_contents($file, $image_data);
$wp_filetype = wp_check_filetype($newFileName, null);
$upload_id = array(
'post_mime_type' => $wp_filetype['type'],
'post_title'=> sanitize_file_name($newFileName),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $upload_id, $file, $post_ID);
if (!is_wp_error($attach_id)) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attachment_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attachment_data );
set_post_thumbnail( $post_ID, $attachment_data );
}
//Return the uploaded image ID, so to use to update the ACF field
}
}
Share
Improve this question
asked Nov 11, 2020 at 18:11
Kirsty MarksKirsty Marks
2571 gold badge3 silver badges13 bronze badges
1
|
1 Answer
Reset to default 1I 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 typeimage
andgallery
fields. The
$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'] . '/' . 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);
}
}
}
}
}
createImage()
function? – joshmoto Commented Nov 14, 2020 at 4:32