Using gravity forms as a front end file up-loader, users can upload gallery images via a form which then saves to a ACF ( advanced custom forms ) gallery field.
It works to a point, the form uploads the images and they are stored in gravity form entry (you can view the images just fine).
The second part of the code below attempts to move the image from the gravity form attachment into the media library as a post attachment
This is where it fails, leaving the image file name but a broken image ( 0kb in size ).
The original code can be found here, but I'm at a loss as to where the problem is : https://joshuadnelson.com/connect-gravity-forms-file-upload-to-acf-gallery-field/
/**
* Attach images uploaded through Gravity Form to ACF Gallery Field
*
* @return void
*/
$gravity_form_id = 10; // gravity form id, or replace {$gravity_form_id} below with this number
add_filter( "gform_after_submission_{$gravity_form_id}", 'jdn_set_post_acf_gallery_field', 10, 2 );
function jdn_set_post_acf_gallery_field( $entry, $form ) {
$gf_images_field_id = 69; // the upload field id
$acf_field_id = 'field_59e260b6e28fd'; // the acf gallery field id
// get post, if there isn't one, bail
if( isset( $entry['post_id'] ) ) {
$post = get_post( $entry['post_id'] );
if( is_null( $post ) )
return;
} else {
return;
}
// Clean up images upload and create array for gallery field
if( isset( $entry[ $gf_images_field_id ] ) ) {
$images = stripslashes( $entry[ $gf_images_field_id ] );
$images = json_decode( $images, true );
if( !empty( $images ) && is_array( $images ) ) {
$gallery = array();
foreach( $images as $key => $value ) {
// NOTE: this is the other function you need: https://gist.github.com/joshuadavidnelson/164a0a0744f0693d5746
if( ! class_exists( 'JDN_Create_Media_File' ) )
break;
// Create the media library attachment and store the attachment id in the gallery array
$create_image = new JDN_Create_Media_File( $value, $post->ID );
$image_id = $create_image->attachment_id;
if( absint( $image_id ) ) {
$gallery[] = $image_id;
}
}
}
}
// Update gallery field with array
if( ! empty( $gallery ) ) {
update_field( $acf_field_id, $gallery, $post->ID );
}
}
__
<?php
class JDN_Create_Media_File {
var $post_id;
var $image_url;
var $wp_upload_url;
var $attachment_id;
/**
* Setup the class variables
*/
public function __construct( $image_url, $post_id = 0 ) {
// Setup class variables
$this->image_url = esc_url( $image_url );
$this->post_id = absint( $post_id );
$this->wp_upload_url = $this->get_wp_upload_url();
$this->attachment_id = $this->attachment_id ?: false;
return $this->create_image_id();
}
/**
* Set the upload directory
*/
private function get_wp_upload_url() {
$wp_upload_dir = wp_upload_dir();
return isset( $wp_upload_dir['url'] ) ? $wp_upload_dir['url'] : false;
}
/**
* Create the image and return the new media upload id.
*
* @see https://gist.github.com/hissy/7352933
*
* @see http://codex.wordpress.org/Function_Reference/wp_insert_attachment#Example
*/
public function create_image_id() {
if( $this->attachment_id )
return $this->attachment_id;
if( empty( $this->image_url ) || empty( $this->wp_upload_url ) )
return false;
$filename = basename( $this->image_url );
$upload_file = wp_upload_bits( $filename, null, file_get_contents( $this->image_url ) );
if ( ! $upload_file['error'] ) {
$wp_filetype = wp_check_filetype( $filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_parent' => $this->post_id,
'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attachment_id = wp_insert_attachment( $attachment, $upload_file['file'], $this->post_id );
if( ! is_wp_error( $attachment_id ) ) {
require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
$this->attachment_id = $attachment_id;
return $attachment_id;
}
}
return false;
} // end function get_image_id
}