Advertisement
adczk

Forminator - set upload field to WP core (non ACF) custom field

May 18th, 2023
778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.89 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Plugin Name: [Forminator] Upload mapping to ACF image field
  4.  * Description: Solves a bug that prevents upload field mapping to ACF image field through post data form
  5.  * Author: Prashant Singh @ WPMUDEV
  6.  * Author URI: https://premium.wpmudev.org
  7.  * License: GPLv2 or later
  8.  */
  9.  
  10. if ( ! defined( 'ABSPATH' ) ) {
  11.     exit;
  12. }
  13.  
  14. if ( defined( 'WP_CLI' ) && WP_CLI ) {
  15.     return;
  16. }
  17.  
  18. add_filter( 'forminator_custom_form_submit_errors', 'wpmudev_map_uplods_acf_post_test', 10, 3 );
  19. function wpmudev_map_uplods_acf_post_test( $submit_errors, $form_id, $field_data_array ) {
  20.     $form_ids = array( 16552 ); //Please change form IDs to your form IDs
  21.     if( !in_array( $form_id, $form_ids ) ){
  22.         return $submit_errors;
  23.     }
  24.  
  25.     $submitted_data = Forminator_CForm_Front_Action::$prepared_data;
  26.    
  27.     if( isset( $submitted_data['postdata-1'] ) && is_array( $submitted_data['postdata-1'] ) ) {
  28.         if( isset( $submitted_data['postdata-1']['post-custom'] ) && is_array( $submitted_data['postdata-1']['post-custom'] ) ) {
  29.             foreach( $submitted_data['postdata-1']['post-custom'] as $post_key => $post_val ) {
  30.                 if( strpos( $post_val['value'], '{upload' ) !== false ) {
  31.                     $field_name = str_replace('{', '', $post_val['value'] );
  32.                     $field_name = str_replace('}', '', $field_name );
  33.                     if ( is_array( $submitted_data[ $field_name ] ) && isset( $submitted_data[ $field_name ]['file'] ) ) {
  34.                         if ( isset( $submitted_data[ $field_name ]['file']['file_url'] ) ) {
  35.                             Forminator_CForm_Front_Action::$prepared_data['postdata-1']['post-custom'][$post_key]['value'] = $submitted_data[ $field_name ]['file']['file_url'];
  36.                         }
  37.                     }
  38.                 }
  39.             }
  40.         }
  41.     }
  42.    
  43.     return $submit_errors;
  44. }
  45.  
  46. add_action( 'forminator_post_data_field_post_saved', 'wpmudev_update_post_acf_uploads_test', 10, 4 );
  47. function wpmudev_update_post_acf_uploads_test( $post_id, $field, $data, $cls ){
  48.     $submitted_data = Forminator_CForm_Front_Action::$prepared_data;
  49.     $form_ids = array( 16552 ); //Please change form IDs to your form IDs
  50.     if ( !in_array( $submitted_data['form_id'], $form_ids ) ) {
  51.       return;  
  52.     }
  53.    
  54.     if( isset( $submitted_data['postdata-1'] ) && is_array( $submitted_data['postdata-1'] ) ) {
  55.         if( isset( $submitted_data['postdata-1']['post-custom'] ) && is_array( $submitted_data['postdata-1']['post-custom'] ) ) {
  56.             foreach( $submitted_data['postdata-1']['post-custom'] as $post_key => $post_val ) {
  57.                 if ( $post_val['key'] == 'View Report' ) { //Please change test_image to your custom field name
  58.                     if ( isset( $post_val['value'] ) ) {
  59.                             update_post_meta( $post_id, $post_val['key'], $post_val['value'] );
  60.                             $mime_type = wp_get_image_mime( $post_val['value'] );
  61.                             if( $mime_type ) {
  62.                                 $update_data = array(
  63.                                     'ID' => $attach_id,
  64.                                     'post_mime_type' => $mime_type,
  65.                                 );
  66.                                 wp_update_post( $update_data );
  67.                             }
  68.                     }
  69.                 }
  70.             }
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement