Last active
August 29, 2015 14:16
-
-
Save rockymenteng/e82b1cd08b747d910384 to your computer and use it in GitHub Desktop.
functions.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Attach images uploaded through Gravity Form to ACF Gallery Field | |
* | |
* @author Joshua David Nelson, josh@joshuadnelson.com | |
* @return void | |
*/ | |
add_filter( "gform_after_submission_3", 'jdn_set_post_acf_gallery_field', 10, 2 ); | |
function jdn_set_post_acf_gallery_field( $entry, $form ) { | |
$gf_images_field_id = 4; // the upload field id | |
$acf_field_id = 'field_5356e6a603b30'; // the acf gallery field id | |
// get post | |
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 ) { | |
if( function_exists( 'jdn_create_image_id' ) ) | |
$image_id = jdn_create_image_id( $value, $post->ID ); | |
if( $image_id ) { | |
$gallery[] = $image_id; | |
} | |
} | |
} | |
} | |
// Update gallery field with array | |
if( ! empty( $gallery ) ) { | |
update_field( $acf_field_id, $gallery, $post->ID ); | |
// Updating post | |
wp_update_post( $post ); | |
} | |
} | |
/** | |
* Other function needed. This is to get image attachment ID | |
* | |
* @author Joshua David Nelson, josh@joshuadnelson.com | |
* Source: https://gist.github.com/joshuadavidnelson/164a0a0744f0693d5746 | |
*/ | |
function jdn_create_image_id( $image_url, $parent_post_id = null ) { | |
if( !isset( $image_url ) ) | |
return false; | |
// Cache info on the wp uploads dir | |
$wp_upload_dir = wp_upload_dir(); | |
// get the file path | |
$path = parse_url( $image_url, PHP_URL_PATH ); | |
// File base name | |
$file_base_name = basename( $image_url ); | |
// Full path | |
$home_path = get_home_path(); | |
$home_path = untrailingslashit( $home_path ); | |
$uploaded_file_path = $home_path . $path; | |
// Check the type of file. We'll use this as the 'post_mime_type'. | |
$filetype = wp_check_filetype( $file_base_name, null ); | |
// error check | |
if( !empty( $filetype ) && is_array( $filetype ) ) { | |
// Create attachment title | |
$post_title = preg_replace( '/\.[^.]+$/', '', $file_base_name ); | |
// Prepare an array of post data for the attachment. | |
$attachment = array( | |
'guid' => $wp_upload_dir['url'] . '/' . basename( $uploaded_file_path ), | |
'post_mime_type' => $filetype['type'], | |
'post_title' => esc_attr( $post_title ), | |
'post_content' => '', | |
'post_status' => 'inherit' | |
); | |
// Set the post parent id if there is one | |
if( !is_null( $parent_post_id ) ) | |
$attachment['post_parent'] = $parent_post_id; | |
// Insert the attachment. | |
$attach_id = wp_insert_attachment( $attachment, $uploaded_file_path ); | |
//Error check | |
if( !is_wp_error( $attach_id ) ) { | |
//Generate wp attachment meta data | |
if( file_exists( ABSPATH . 'wp-admin/includes/image.php') && file_exists( ABSPATH . 'wp-admin/includes/media.php') ) { | |
require_once( ABSPATH . 'wp-admin/includes/image.php' ); | |
require_once( ABSPATH . 'wp-admin/includes/media.php' ); | |
$attach_data = wp_generate_attachment_metadata( $attach_id, $uploaded_file_path ); | |
wp_update_attachment_metadata( $attach_id, $attach_data ); | |
} // end if file exists check | |
} // end if error check | |
return $attach_id; | |
} // end if $$filetype | |
} // end function get_image_id | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rocky, see the response on the blog post. I made a change to the id function and there is another gist that might work for you - my example assumed you were creating a new post on submit (so it needed a post_id in the entry object).