Created
April 4, 2017 19:27
-
-
Save joshuadavidnelson/0b9b0783d608264f8e0dcd49ea226999 to your computer and use it in GitHub Desktop.
Connect a Gravity Form File Upload Field to an ACF Image Field. See https://joshuadnelson.com/connect-gravity-forms-file-upload-to-acf-gallery-field/
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 | |
/** | |
* Connect a Gravity Form File Upload Field to an ACF Image Field. | |
* @see https://joshuadnelson.com/connect-gravity-forms-file-upload-to-acf-gallery-field/ | |
*/ | |
$gravity_form_id = 1; // gravity form id, or replace {$gravity_form_id} below with this number | |
add_filter( "gform_after_submission_{$gravity_form_id}", 'jdn_set_acf_gallery_field', 10, 2 ); | |
function jdn_set_acf_gallery_field( $entry, $form ) { | |
$gf_images_field_id = 7; // the upload field id | |
$acf_field_id = 'field_58ce208d568b6'; // 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 add to the ACF field | |
if( isset( $entry[ $gf_images_field_id ] ) ) { | |
$image_url = stripslashes( $entry[ $gf_images_field_id ] ); | |
$create_image = new Create_Media_File( $image_url, $post->ID ); | |
$image_id = $create_image->attachment_id; | |
// Update the ACF field | |
if( ! empty( $image_id ) ) { | |
update_field( $acf_field_id, $image_id, $post->ID ); | |
// Updating post | |
wp_update_post( $post ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment