Skip to content

Instantly share code, notes, and snippets.

@mikesale
Created October 3, 2018 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikesale/53dfd86111f0f6f02a49a51d429ba2d1 to your computer and use it in GitHub Desktop.
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
<?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;
}
@mikesale
Copy link
Author

mikesale commented Oct 3, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment