Created
October 3, 2018 22:21
-
-
Save mikesale/53dfd86111f0f6f02a49a51d429ba2d1 to your computer and use it in GitHub Desktop.
A function with filter for wp to enable the use of merge tags in gravity forms fields before form submission
This file contains hidden or 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
<?php | |
/** | |
* Merge Tags as Dynamic Population Parameters | |
* http://gravitywiz.com/dynamic-products-via-post-meta/ | |
*/ | |
add_filter('gform_pre_render', 'gw_prepopluate_merge_tags'); | |
function gw_prepopluate_merge_tags($form) { | |
$filter_names = array(); | |
foreach($form['fields'] as &$field) { | |
if(!rgar($field, 'allowsPrepopulate')) | |
continue; | |
// complex fields store inputName in the "name" property of the inputs array | |
if(is_array(rgar($field, 'inputs')) && $field['type'] != 'checkbox') { | |
foreach($field['inputs'] as $input) { | |
if(rgar($input, 'name')) | |
$filter_names[] = array('type' => $field['type'], 'name' => rgar($input, 'name')); | |
} | |
} else { | |
$filter_names[] = array('type' => $field['type'], 'name' => rgar($field, 'inputName')); | |
} | |
} | |
foreach($filter_names as $filter_name) { | |
$filtered_name = GFCommon::replace_variables_prepopulate($filter_name['name']); | |
if($filter_name['name'] == $filtered_name) | |
continue; | |
add_filter("gform_field_value_{$filter_name['name']}", create_function("", "return '$filtered_name';")); | |
} | |
return $form; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should add this to your child theme's functions.php, not replace it or add it to the base theme because it will be overwritten on update.