Created
June 13, 2018 13:18
-
-
Save mgussekloo/ae50018db025dcf808666b8c316fefe3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Gravity forms merge tags on multi page form | |
There seems to be a bug in Gravity Forms, when you're using custom Merge Tags; | |
normally, an empty input value will be replaced by the | |
"default value", if you use the Default Value option in Gravity Forms. | |
On multipage forms this means that fields that were left empty _intentionally_ on page one will | |
be overwritten by the default value as soon as you load page two (and so on). | |
This is a fix I came up with. | |
Replace "CustomMergeTagName" with (part of) your custom Merge Tag, or leave those lines out entirely | |
if you want to apply this fix to ALL (default and custom) Merge Tags. | |
*/ | |
add_filter( 'gform_field_content', function ( $field_content, $field, $value, $entry_id, $form_id ) { | |
$current_page = isset( GFFormDisplay::$submission[$form_id] ) ? GFFormDisplay::$submission[$form_id]['page_number'] : 1; | |
if( rgar( $field, 'pageNumber' ) != $current_page ) { | |
if (isset($field['inputs']) && is_array($field['inputs'])) { | |
foreach ($field['inputs'] as $input) { | |
$postName = 'input_' . $input['id']; | |
if ( | |
isset($input['defaultValue']) && | |
strpos($input['defaultValue'], 'CustomMergeTagName') !== false && | |
isset($_POST[$postName]) && | |
$_POST[$postName] == '' | |
) { | |
$val = esc_html($value); | |
$field_content = preg_replace('/ value=\'' . $val . '\' /',' value=\'\' ',$field_content); | |
} | |
} | |
} | |
$postName = 'input_' . $field['id']; | |
if ( | |
isset($field['defaultValue']) && | |
strpos($input['defaultValue'], 'CustomMergeTagName') !== false && | |
isset($_POST[$postName]) && | |
$_POST[$postName] == '' | |
) { | |
$val = esc_html($value); | |
$field_content = preg_replace('/ value=\'' . $val . '\' /',' value=\'\' ',$field_content); | |
} | |
} | |
return $field_content; | |
}, 10, 5); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment