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