Adding custom merge tags to the Gravity forms

Author - Komal
2.03.2017
|
2 Comments
|

Merge Tags in gravity forms are the tags that are used to dynamically populate the field values in the form content in gravity forms like IP address, browser name, logged in user name etc . There are many merge tags available in the gravity forms by default.
1
But sometimes there may be situation where we want certain values to fill in the form content dynamically just like these merge tags. So if the merge tag is not available for that value then we can also add our own merge tag to the list of the tags and those tags are known as Custom Merge Tags.

Creating Custom Merge Tags
To create the custom merge tag, we need to add a hook to our functions.php file that will add our created tag to that list.

For Example:
This code is to create the custom merge tag to populate the site name dynamically to the gravity forms from Settings -> General. Here, add_action is the wordpress hook to add function add_merge_tags to ‘gform_admin_pre_render’ filter so that it can be executed for each gravity form.
gform_admin_pre_render is a filter of gravity forms that is executed before the entry detail is displayed. We can use it to modify the Form Object prior to rendering the entry.
add_merge_tags function takes form as the parameter and adds the new custom merge tag labelled as “Site Name” to the merge tags drop down on the right side of each field in the gravity forms.

add_action( 'gform_admin_pre_render', 'add_merge_tags' );
function add_merge_tags($form) { ?>    
    < script type = "text/javascript" >
        gform.addFilter('gform_merge_tags', 'add_merge_tags');

    function add_merge_tags(mergeTags, elementId, hideAllFields, excludeFieldTypes, isPrepop, option) {
        mergeTags["custom"].tags.push({
            tag: '{Site_Name}',
            label: 'Site Name'
        });
        return mergeTags;
    } < /script> 
<? php
    //return the form object from the php hook  
    return $form;
}

This above code will create the custom merge tag labelled as “Site Name” to the list.

Add the value to be replaced for the Custom Merge Tag
This below code is to replace the value of the custom merge tag that we created above with the specific value. Here get_bloginfo( ‘name’ ); gives the name of the site from Settings -> General and that name is used to replace the custom merge tag wherever it is used.

add_filter('gform_replace_merge_tags', 'replace_site_name', 10, 7);

function replace_site_name($text, $form, $entry, $url_encode, $esc_html, $nl2br, $format) {
    $custom_merge_tag = '{Site_Name}';
    if (strpos($text, $custom_merge_tag) === false) {
        return $text;
    }
    $siteName = get_bloginfo('name');
    $text = str_replace($custom_merge_tag, $siteName, $text);
    return $text;
}

2
Now wherever we want site name to be populated dynamically in the form content, we can simply add this custom merge tag there, it will be replaced by the site name.

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

2 responses on “Adding custom merge tags to the Gravity forms

  1. Kirill says:

    This is not working.
    The first block of code (add_merge_tags) is obviously incorrect.

    • Webner says:

      add_merge_tags is a custom function that will have to be attached to ‘gform_admin_pre_render’ filter of the gravity form so that it will also get executed each time before the entry detail is displayed on the gravity form. You can simply do so by the wordpress hook ‘add_action’.
      Just add this below line before add_merge_tags function:
      add_action( ‘gform_admin_pre_render’, ‘add_merge_tags’ );

Leave a Reply

Your email address will not be published. Required fields are marked *