Forum Replies Created

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter kiterfred

    (@kiterfred)

    Hi

    just wanted to let you know that i found a solution for my case.
    Before I started this topic I also tried to use the upload field in combination with ACF but this didn’t work. So after another search i found someone having this issue and got a solution. I changed a line and got my solution. I didn’t find the website where I found the code but here is my solution using the following code as mu-plugin:

    <?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 ) {
        $fform_id =  1547 ; //Please change form ID to your form ID
    	if( $form_id != $fform_id ){
    		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_id = 1547; //Please change form ID to your form ID
    	if ( $submitted_data['form_id'] != $form_id ) {
    	  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'] == 'upload-1' ) { //Please change upload-1 to your custom field name
    				    if ( isset( $post_val['value'] ) ) {
        					$attach_id = attachment_url_to_postid( $post_val['value'] );
        					if( $attach_id ) {
        						update_field($post_val['key'], $attach_id, $post_id);
        						$mime_type = wp_get_image_mime( $post_val['value'] );
        						if( $mime_type ) {
        							$update_data = array(
        								'ID' => $attach_id,
        								'post_mime_type' => $mime_type,
        							);
    								set_post_thumbnail( $post_id, $attach_id );  //this sets the uploaded image as featured image of the post
        							wp_update_post( $update_data );
        						}
        					}
    				    }
    				}
    			}
    		}
    	}	
    }
    Thread Starter kiterfred

    (@kiterfred)

    Hi Zafer,
    here is the export of my form:
    https://pastebin.com/R9cUutaN

    Kind regards
    Kiterfred

    Thread Starter kiterfred

    (@kiterfred)

    Hi Nithin,

    i tried the workaround as described but it didn’t solve my issue. When loading the form with ajax i get new issues i have to solve which aren’t part of my real problem.

    Is it possible to set a standard image via code, when a person didn’t set one? This would also solve my issue.

    Thank you for your help

    Thread Starter kiterfred

    (@kiterfred)

    Hey thanks for your fast reply. Hope you have a merry christmas.

    I was able to test ist, but your code snippet didn’t solve the problem. I updated the 3 lines with my form id and put the code in a file in the mu-plugin folder. did i miss something?

    Thread Starter kiterfred

    (@kiterfred)

    Thank you so much for your help.

    This solves my problem.

    Thread Starter kiterfred

    (@kiterfred)

    Hey Nithin,

    thank you for your reply.
    I made a minimal example of my problem.
    You can get the formnator form and the acf export from

    https://www.dropbox.com/sh/881hi87nf7nbqht/AADmFqeLFPqOTXG9FInixhv6a?dl=0

    Version of
    ACF is 6.0.5
    Forminator is 1.20.1
    Wordpress is 6.1.1

    If i can do more to help you please write me.

Viewing 6 replies - 1 through 6 (of 6 total)