Created
May 26, 2023 10:34
-
-
Save adczk/671c79c2ca431bbf79e4b638484c2436 to your computer and use it in GitHub Desktop.
Forminator - map upload to ACF gallery via postdata
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 | |
| /** | |
| * Plugin Name: Forminator - map upload to ACF gallery via postdata | |
| * Description: Forminator - map upload to ACF gallery via postdata | |
| * Author: adczk | |
| * Author URI: https://premium.wpmudev.org | |
| * License: GPLv2 or later | |
| * | |
| * USE A MU PLUGIN | |
| * | |
| * UPLOAD field must be set to "multiple" | |
| * and must already be correctly mapped in postdata field to ACF field | |
| * config: see comments in code (lines 20-21) | |
| * | |
| * Tested with Forminator Pro 1.24 | |
| */ | |
| add_action( 'forminator_post_data_field_post_saved', 'wpmudev_update_post_acf_uploads', 10, 4 ); | |
| function wpmudev_update_post_acf_uploads( $post_id, $field, $data, $cls ) { | |
| $submitted_data = Forminator_CForm_Front_Action::$prepared_data; | |
| $acf_fields = array( 'mygal' ); // set your ACF gallery fields names | |
| $form_ids = array( 16502 ); // set your form IDs | |
| if ( !in_array( $submitted_data['form_id'], $form_ids ) ) { | |
| return; | |
| } | |
| $acf_field = ''; | |
| $upload_field = ''; | |
| $img_ids = array(); | |
| if ( isset( $submitted_data['postdata-1']['post-custom'] ) ){ | |
| foreach ( $submitted_data['postdata-1']['post-custom'] as $pkey=>$pval ) { | |
| if ( (in_array($pval['key'], $acf_fields)) && (strpos( $pval['value'], 'upload') !== false ) ) { | |
| $acf_field = $pval['key']; | |
| $upload_field = str_replace( "}", "", str_replace( "{", "", $pval['value'] ) ); | |
| foreach ( $submitted_data[$upload_field]['file']['file_url'] as $ikey=>$iurl ) { | |
| $img_ids[] = attachment_url_to_postid( $iurl ); | |
| } | |
| if ( ! empty( $img_ids ) ) { | |
| update_field( $acf_field, $img_ids, $post_id); | |
| } | |
| } | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment