Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Plugin Name: [Forminator] Upload mapping to ACF image field
- * Description: Solves a bug that prevents upload field mapping to ACF image field through post data form
- * Author: Prashant Singh @ WPMUDEV
- * Author URI: https://premium.wpmudev.org
- * License: GPLv2 or later
- */
- if ( ! defined( 'ABSPATH' ) ) {
- exit;
- }
- if ( defined( 'WP_CLI' ) && WP_CLI ) {
- return;
- }
- add_filter( 'forminator_custom_form_submit_errors', 'wpmudev_map_uplods_acf_post_test', 10, 3 );
- function wpmudev_map_uplods_acf_post_test( $submit_errors, $form_id, $field_data_array ) {
- $form_ids = array( 16552 ); //Please change form IDs to your form IDs
- if( !in_array( $form_id, $form_ids ) ){
- return $submit_errors;
- }
- $submitted_data = Forminator_CForm_Front_Action::$prepared_data;
- if( isset( $submitted_data['postdata-1'] ) && is_array( $submitted_data['postdata-1'] ) ) {
- if( isset( $submitted_data['postdata-1']['post-custom'] ) && is_array( $submitted_data['postdata-1']['post-custom'] ) ) {
- foreach( $submitted_data['postdata-1']['post-custom'] as $post_key => $post_val ) {
- if( strpos( $post_val['value'], '{upload' ) !== false ) {
- $field_name = str_replace('{', '', $post_val['value'] );
- $field_name = str_replace('}', '', $field_name );
- if ( is_array( $submitted_data[ $field_name ] ) && isset( $submitted_data[ $field_name ]['file'] ) ) {
- if ( isset( $submitted_data[ $field_name ]['file']['file_url'] ) ) {
- Forminator_CForm_Front_Action::$prepared_data['postdata-1']['post-custom'][$post_key]['value'] = $submitted_data[ $field_name ]['file']['file_url'];
- }
- }
- }
- }
- }
- }
- return $submit_errors;
- }
- add_action( 'forminator_post_data_field_post_saved', 'wpmudev_update_post_acf_uploads_test', 10, 4 );
- function wpmudev_update_post_acf_uploads_test( $post_id, $field, $data, $cls ){
- $submitted_data = Forminator_CForm_Front_Action::$prepared_data;
- $form_ids = array( 16552 ); //Please change form IDs to your form IDs
- if ( !in_array( $submitted_data['form_id'], $form_ids ) ) {
- return;
- }
- if( isset( $submitted_data['postdata-1'] ) && is_array( $submitted_data['postdata-1'] ) ) {
- if( isset( $submitted_data['postdata-1']['post-custom'] ) && is_array( $submitted_data['postdata-1']['post-custom'] ) ) {
- foreach( $submitted_data['postdata-1']['post-custom'] as $post_key => $post_val ) {
- if ( $post_val['key'] == 'View Report' ) { //Please change test_image to your custom field name
- if ( isset( $post_val['value'] ) ) {
- update_post_meta( $post_id, $post_val['key'], $post_val['value'] );
- $mime_type = wp_get_image_mime( $post_val['value'] );
- if( $mime_type ) {
- $update_data = array(
- 'ID' => $attach_id,
- 'post_mime_type' => $mime_type,
- );
- wp_update_post( $update_data );
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement