Last active
April 29, 2020 18:41
-
-
Save yanknudtskov/35a0783fce1bd815e12dfad00508643f to your computer and use it in GitHub Desktop.
Connect Gravity Forms Multi File Upload and Advanced Custom Fields Repeating File-Field #gravity-forms #acf
This file contains hidden or 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 Gravity Forms Multi File Upload and Advanced Custom Fields Repeating File-Field and Gallery Field | |
* REMEMBER: Set ID correctly for forms and fields AND set acf field names | |
*/ | |
add_action( 'gform_after_submission_4', 'add_form_after_submission', 10, 2 ); | |
function add_form_after_submission( $entry, $form ) { | |
//getting post | |
$post = get_post( $entry['post_id'] ); | |
if( $post != null ) { | |
// Fotos, Gallery Field | |
if( rgar( $entry, '30' ) != null && !empty(rgar( $entry, '30' ) ) ) { | |
gravity_forms_acf_add_files_to_media_library( $post, $entry, '30', 'acf_production_gallery' ); // @TODO | |
} | |
// Dokumenter, Repeating File-Field | |
if( rgar( $entry, '51' ) != null && !empty(rgar( $entry, '51' ) ) ) { | |
gravity_forms_acf_add_files_to_media_library( $post, $entry, '51', 'acf_production_documents', 'acf_production_documents_file' ); // @TODO | |
} | |
} | |
return; | |
} | |
function gravity_forms_acf_add_files_to_media_library( $post, $entry, $gravity_form_field_id, $acf_field_name, $acf_sub_field_name = null ) { | |
$array_file_urls = stripslashes( rgar( $entry, $gravity_form_field_id ) ); | |
$array_file_urls = json_decode( $array_file_urls, true ); | |
add_files_to_media_library( $post, $array_file_urls, $acf_field_name, $acf_sub_field_name ); | |
} | |
function add_files_to_media_library( $post, $array_file_urls, $acf_field_name, $acf_sub_field_name = null ) { | |
$acf_file_id_array = array(); | |
$wp_upload_dir = wp_upload_dir(); | |
foreach( $array_file_urls as $key => $file_url ) { | |
// $file = rtrim( ABSPATH, '/' ) . parse_url( $file_url , PHP_URL_PATH ); | |
$filename = basename( $file_url ); | |
$upload_file = wp_upload_bits( $filename, null, file_get_contents( $file_url ) ); | |
if ( !$upload_file['error'] ) { | |
$wp_filetype = wp_check_filetype( $filename, null ); | |
$attachment = array( | |
'guid' => $wp_upload_dir['url'] . '/' . $filename, | |
'post_mime_type' => $wp_filetype['type'], | |
'post_parent' => $post->ID, | |
'post_title' => preg_replace('/\.[^.]+$/', '', $filename), | |
'post_content' => '', | |
'post_status' => 'inherit' | |
); | |
$attachment_id = wp_insert_attachment( $attachment, $upload_file['file'], $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 ); | |
if( $acf_sub_field_name != null ) { | |
array_push( $acf_file_id_array, array( $acf_sub_field_name => $attachment_id ) ); | |
} else { | |
array_push( $acf_file_id_array, $attachment_id ); | |
} | |
} | |
} | |
} | |
if( !empty( $acf_file_id_array ) ) { | |
update_field( $acf_field_name, $acf_file_id_array, $post->ID ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment