• Resolved fdcommunication

    (@fdcommunication)


    Hello,

    <?php
    add_filter( 'forminator_field_hidden_field_value', function( $value, $saved_value, $field ){
    	if( ! empty( $field['default_value'] ) && 'custom_value' === $field['default_value'] && strpos( $value, 'acf_' ) !== false ){
    		$field_keys = explode('acf_', $value );
    		$post_id = false;//default is current post/page
    		$value = get_field( $field_keys[1], $post_id );
    	}
    	return $value;
    }, 10, 3 );

    I use this code to display ACF data in a hidden field in my form email. It worked perfectly until the end of June. I think an update to your plugin or that of ACF made the code non-functional. Can you check this code and suggest a fix?

    Thank you !

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi @fdcommunication

    I hope you are doing well today.

    Could you test this updated version and see if this work?

    add_filter( 'forminator_prepared_data', 'wpmudev_update_hidden_field_val_fix', 10, 2 );
    function wpmudev_update_hidden_field_val_fix( $prepared_data, $module_object ){
        $form_ids = array(6); //Please change the form ID
    	if ( !in_array( $module_object->id, $form_ids ) ) {
    		return $prepared_data;
    	}
        
        foreach ( $prepared_data as $key => $value ) {
            if ( strpos( $key, 'hidden-1' ) !== false ) {
                $prepared_data[$key] = sanitize_text_field( $_POST[$key] );
            }
        }
    
    	return $prepared_data;
    }
    
    add_action( 'forminator_custom_form_submit_before_set_fields', 'wpmudev_change_hidden_field_data_fix', 10, 3 );
    function wpmudev_change_hidden_field_data_fix( $entry, $module_id, $field_data_array ) {
        $form_ids = array(6); //Please change the form ID
         if ( !in_array( $module_id, $form_ids ) ) {
    		return;
          }
        
        foreach ( $field_data_array as $key => $value ) {
            if ( strpos( $value['name'], 'hidden-1' ) !== false ) {
                Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = sanitize_text_field( $_POST[$value['name']] );
            }
        }
    
    }

    Note: In this snippet please change 6 to your form’s ID and hidden-1 to your hidden field’s ID.

    Kind Regards,
    Kris

    Thread Starter fdcommunication

    (@fdcommunication)

    Hello,

    Thank you for your reply.

    So I installed the code, and gave it a try.
    But it didn’t work, my email showed me “acf_reference_du_circuit” for “hidden-2”. Do I need to change anything in the hidden field setting?
    Currently the value is “Custom value” and the custom value is “acf_reference_du_circuit”.

    Plugin Support Laura – WPMU DEV Support

    (@wpmudev-support8)

    Hi @fdcommunication

    I hope you’re well today!

    I checked both codes and it seems there was a mistake on our end – we overlooked the “ACF part” in the shared code and only shared “generic” code related to the issue. I apologize for that.

    As for the issue itself – code needs to be updated because for some time already hidden fields’ values cannot be overwritten “just like that”; they are sanitized so they can only contain values assigned to them in field settings for safety reasons.

    Code shared by my colleague is meant to help with that but was not modified to actually read data from related ACF field.

    Below is an update version that should work just fine. I assumed that the idea here is that

    – you put ACF field name, prefixed with “acf_” as custom value of your hidden field
    – and then code simply reads value of that field for “current post”

    It seems to be working fine in my tests and you shouldn’t need to make any changes in the form.

    Here is the new code:

    <?php 
    
    add_filter( 'forminator_prepared_data', 'wpmudev_update_hidden_field_val_fix', 10, 2 );
    function wpmudev_update_hidden_field_val_fix( $prepared_data, $module_object ){
    
        $form_ids = array(123); //Please change the form ID
    	if ( !in_array( $module_object->id, $form_ids ) ) {
    		return $prepared_data;
    	}
        
        foreach ( $prepared_data as $key => $value ) {
            if ( strpos( $key, 'hidden-2' ) !== false ) {
    			if ( strpos( $value, 'acf_' ) !== false  ) {
    				$field_keys = explode('acf_', $value );
    				$post_id = $prepared_data['page_id'];
    				$prepared_data[$key] = sanitize_text_field( get_field( $field_keys[1], $post_id ) );
    				$_POST['_form_acf_post_id'] = $post_id;
    				
    			}
            }
        }
    
    	return $prepared_data;
    }
    
    add_action( 'forminator_custom_form_submit_before_set_fields', 'wpmudev_change_hidden_field_data_fix', 10, 3 );
    function wpmudev_change_hidden_field_data_fix( $entry, $module_id, $field_data_array ) {
    
        $form_ids = array(123); //Please change the form ID
         if ( !in_array( $module_id, $form_ids ) ) {
    		return;
          }
    
        foreach ( $field_data_array as $key => $value ) {
            if ( strpos( $value['name'], 'hidden-2' ) !== false ) {
    			if ( strpos( $value['value'], 'acf_' ) !== false )  {
    				$field_keys = explode( 'acf_', $value['value'] );
    				$post_id = $_POST['_form_acf_post_id'];
    				Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = sanitize_text_field( get_field( $field_keys[1], $post_id ) );
    			} 
            }
        }
    
    }

    Note:

    1. use it instead code shared previously (and your old code)
    2. make sure to set correct form ID by replacing both 123 values with Form ID (form ID is the number you see in form’s shortcode)
    3. and I assumed field is “hidden-2”, if it’s different also update both occurences of “hidden-2” accordingly.

    Best regards,
    Adam

    Thread Starter fdcommunication

    (@fdcommunication)

    It works ! Thank you very much.

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

The topic ‘Hidden field getting data from ACF’ is closed to new replies.