Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- }
- $gravity_form_id = 1; // gravity form id, or replace {$gravity_form_id} below with this number
- add_filter( "gform_after_submission_1", 'jdn_set_post_acf_gallery_field', 10, 2 );
- function jdn_set_post_acf_gallery_field( $entry, $form ) {
- $gf_images_field_id = 3; // the upload field id
- $acf_field_id = 'field_5de3b8157dffc'; // 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_field( $acf_field_id, $gallery, $post->ID );
- }
Advertisement
Add Comment
Please, Sign In to add comment