Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helveticadomes/7a22ff705b803961f9145e14b9796625 to your computer and use it in GitHub Desktop.
Save helveticadomes/7a22ff705b803961f9145e14b9796625 to your computer and use it in GitHub Desktop.
Gravity Perks // GP Media Library // ACF // Auto-attach Uploaded Files to Post created by "GF-Advanced Post Creation Add-On"
<?php
/**
* Gravity Perks // GP Media Library // ACF
* Auto-attach Uploaded Files to Post created by "GF-Advanced Post Creation Add-On"
* this function help if you wish to attach the Files to the Post you generate
* If you use ACF and limit File/Image/Gallery Field (Files uploaded to this post) this is necessary
*
* Install & Activate GP Media Library, Install & Activate Advanced Post Creation Add-On
* use GF-Upload-Field > Perk > Upload to Media Libray checkbox:true
* at Post Creation > Feed Settings => dont use Media Library (empty Select File Upload Fields)
* at Post Creation > Feed Settings => use your ACF Fieldname and select File Upload Field at Dropdown
* Set ACF Field Return Value to ID
* Enjoy that you can limit ACF Field for Files uploaded to this post
*
* http://gravitywiz.com/documentation/gp-media-library/
* https://gravitywiz.com/uploading-files-to-advanced-custom-fields-using-the-gravity-forms-advanced-post-creation-add-on/
* https://docs.gravityforms.com/advanced-post-creation-add-on-using-third-party-post-types/
*/
add_action( 'gform_advancedpostcreation_post_after_creation', function( $post_id, $feed, $entry, $form ) {
if( is_callable( 'gp_media_library' ) ) {
$file_ids = gp_media_library()->get_file_ids_by_entry( $entry, $form );
if( $file_ids ) {
foreach( $file_ids as $field_id => $_file_ids ) {
foreach( $_file_ids as $file_id ) {
wp_update_post( array(
'ID' => $file_id,
'post_parent' => $post_id
) );
}
}
}
}
}, 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment