Created
March 25, 2015 16:43
-
-
Save yratof/2dc20bd578ea4664bd28 to your computer and use it in GitHub Desktop.
Advanced Custom Fields - Update Gallery with Gravity Forms
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
$gravity_form_id = 6; // 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 = 42; // the upload field id | |
$acf_field_id = 'field_54804833b3067'; // 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) { | |
// NOTE: this is the other function you need: https://gist.github.com/joshuadavidnelson/164a0a0744f0693d5746 | |
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); | |
} | |
} |
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
/** | |
* Create the image attachment and return the new media upload id. | |
* | |
* @since 1.0.0 | |
* @see http://codex.wordpress.org/Function_Reference/wp_insert_attachment#Example | |
*/ | |
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 | |
if( site_url() != home_url() ) { | |
$home_path = dirname( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) ); | |
} else { | |
$home_path = dirname( dirname( dirname( dirname( __FILE__ ) ) ) ); | |
} | |
$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; | |
} else { | |
return false; | |
} // 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
@paxjah Uploads do work, but I get an error messages concerning arrays, am I doing something wrong (using a multi-upload GF field connecting to an ACF gallery field (array)) - am I doing something wrong?
Warning: pathinfo() expects parameter 1 to be string, array given in /xxx/xxx/xxx/xxx/xxxx.de/wp-content/plugins/gravityforms/forms_model.php on line 4375