Did you know that Gravity Forms provides a handy shortcode that allows you to display different content depending the data the user has entered in their submission? It’s a bit of a hidden gem.
How does it work? The shortcode allows you to create a condition (based on user input) that will evaluate to true or false. If the condition is true, the content within the shortcode will be displayed. If it is false, the content will not be displayed.
Gravity Forms’ conditional shortcode can be used anywhere WordPress shortcodes are supported. Here is a list of the most useful locations:
- Notifications
- Confirmations
- HTML Fields
with the GP Preview Submission perk - WordPress Post Content
with Gravity Forms Post Content Merge Tags snippet - Post Content Template and Post Title Content Template
available via the Post Title and Post Body field settings respectively, more info
Quick Example
You have a few products that users can request additional information about. The user selects the product they’d like to learn more about.
You’ve used conditional shortcodes on the default confirmation to display a bit of information about the selected product.
This is what the above confirmation would generate if “Gravity Perks” was selected.
Usage Instructions
Let’s have a look at the different parameters that are available to the conditional shortcode. You can copy and paste the template shortcode below. Fill in the values for each parameter as needed.
[gravityforms action="conditional" merge_tag="" condition="" value=""]
Content you would like to conditionally display.
[/gravityforms]
Parameters
action (string) (required)
The action that will be performed. Use
conditional
for conditional shortcodes.merge_tag (string) (required)
Specify the merge tag to be evaluated by the condition and the value.
You can find a full list of form-specific merge tags while editing a notification or confirmation to the right of the content editor. It looks like this:
condition (string) (required)
Enter the condition to be used to test the content of the merge tag with the value you specify. Possible conditions include:
- isnot
- not_equal
- is
- greater_than
- less_than
- contains
- starts_with
- ends_with
value (string) (required)
The value that will be compared against the merge_tag based on the condition.
The final “parameter” is the content. This is represented by any text you include inside the opening [gravityforms]
and closing [/gravityforms]
shortcode tags. This text can include almost anything; from HTML to Gravity Forms merge tags.
Examples
Show Content Only if Value Exists
[gravityforms action="conditional" merge_tag="{My Field:1}" condition="not_equal" value=""]
My Field Label: {My Field:1}
[/gravityforms]
Display User-requested Information
[gravityforms action="conditional" merge_tag="{More information on Gravity Perks:1}" condition="is" value="Gravity Perks"]
<a href="http://gravityperks.com">Take your forms to another universe with Gravity Perks!</a>
[/gravityforms]
If you are using multiple conditional shortcodes, you may notice some spacing issues when unmet conditionals display nothing. You can fix these issues with one of two methods.
- Collapse the shortcodes down so that there is no whitespace between shortcodes.
- Select “Disable Auto-formatting” (when available) and wrap everything in tags.
How’d we do?
There are so many ways to use Gravity Forms’ conditional shortcodes. We’d love to hear how you are using them. Let us know in the comments below.
For more information, check out Gravity Forms’ documentation for conditional shortcodes here: https://www.gravityhelp.com/documentation/article/shortcodes/#conditional-shortcode