-
-
Save groupewibi/3e06bde3a18c98efc3fc783a6f775d98 to your computer and use it in GitHub Desktop.
Connect a Gravity Form upload field to ACF Gallery Field without a *New* Post Creation on Submission, requires the JDN_Create_Media_File class from https://gist.github.com/joshuadavidnelson/164a0a0744f0693d5746
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, [email protected] | |
* @return void | |
*/ | |
$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_post_acf_gallery_field', 10, 2 ); | |
function jdn_set_post_acf_gallery_field( $entry, $form ) { | |
$gf_images_field_id = 1; // the upload field id | |
$acf_field_id = 'field_546d0ad42e7f0'; // the acf gallery field id | |
$post_id = 1; // IMPORTANT - ACF fields are associated with a post, you'll need either to provide a post id here | |
/// or create a field to select/input the post id in the form. | |
// 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; | |
$image_id = new JDN_Create_Media_File( $value ); | |
if( $image_id ) { | |
$gallery[] = $image_id; | |
} | |
} | |
} | |
// Update gallery field with array | |
if( ! empty( $gallery ) ) { | |
update_field( $acf_field_id, $gallery, $post_id ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment