Adding Gravity Form Merge Field Modifiers

Gravity Forms does all kinds of nice things with merge fields. These mail-merge-like chunks make it really easy to create content, send custom messages, or redirect people using the data submitted via forms.

When your plain data doesn’t do what you need, there are a number of useful built in modifiers. I’ve mainly used the value modifier in the past but the label modifier looks pretty useful as well.

Looking at the merge field example below, I’d be targeting the question where I asked about their favorite cat (form field #2) and I’m going to write out the value. The person filling out the form might see the option cat but I could set the value to an image representing a cat or make a little div class to make a cat. A stupid example perhaps but that simple pattern opens up all kinds of opportunities.

{Your favorite pet:2:value}

Sometimes though the thing you need isn’t there. Luckily, Gravity Forms lets you write your own modifier. In this case, Jim wanted to make a link to a particular tag but had problems when the tag had a space in it. You can’t set values in tags and, unlike categories, you can’t get the tag ID as a modifier. A quick Google search led me to this example in the Gravity Forms documentation. I modified it to replaces spaces with dashes.

add_filter( 'gform_merge_tag_filter', function ( $value, $merge_tag, $modifier, $field, $raw_value, $format ) {
    if ( $merge_tag != 'all_fields' && $modifier == 'urlmaker' ) {//I name it urlmaker but it could be whatever you want
        $value = sanitize_title($value);//better cleansing thanks to Alan's comment
    return $value;
}, 10, 6 );

You’d use it in a merge like this. So you can see I’m getting both the plain text for people to see and using the dashed version for the URL.

<a href="tag/{Tags:5:urlmaker}">{Tags:5}</a>

You can then add this as a plugin or to your theme’s functions file. The nice thing is I can now use it with any field as a modifier. I could polish it up a bit more to make things lower case etc. and I’d have something I could use any time I wanted the value to be a CSS class.

Here’s that version as a plugin.

Plugin Name: Gravity Form nodash modifier
Plugin URI:  
Description: Adds a modifier (nodash) to the merge tags in gravity form that makes the value lower case and replaces spaces with dashes
Version:     1.0
Author:      Tom Woodward
Author URI:
License:     GPL2
License URI:
Domain Path: /languages
Text Domain: my-toolset

defined( 'ABSPATH' ) or die( 'No script kiddies please!' );

add_filter( 'gform_merge_tag_filter', function ( $value, $merge_tag, $modifier, $field, $raw_value, $format ) {
    if ( $merge_tag != 'all_fields' && $modifier == 'nodash' ) {
        $value = sanitize_title($value);//fixed thanks to Alan's comment
    return $value;
}, 10, 6 );

4 thoughts on “Adding Gravity Form Merge Field Modifiers

  1. This is awesome, I was just starting to blog my adventures with Gravity Forms, and I have to say two weeks in I feel like these posts you have been writing for years finally make sense 🙂 I appreciate the pace and being able to truly have a thought out model for the library before jumping in too deep and having to re-work everything afterwards

  2. Its really great to watch folks expand their WP chops from your series into the power zone using filters and hooks. This is something undervalued about WP that you can modify almost anything it does by tapping into the flow.

    One tiny suggestion is that rather than doing string replacement and lower casing the input would be to use the function like

    $value = sanitize_title($value);

    To capture a few more input edge cases (apostrophes etc)

    1. Good point. Modified. I really don’t know why I didn’t do that to begin with. I think I got caught up trying to walk things through step-by-step and forgot to use the tools at hand. Always better to use the tools WP hands you.

Comments are closed.