Gravity Forms Advanced Conditional Shortcode
Display conditional Gravity Forms content anywhere shortcodes are supported with multiple conditions.
Overview
Did you know that Gravity Forms provides a shortcode to display different content based on what the user has entered in their submission? If not, check out our Gravity Forms Conditional Shortcode article for an overview how it works. Afterwards, hop back into this article to learn how to get the most out of it.
Just getting started with Gravity Forms Shortcodes? Check out our comprehensive guide.
One limitation of the built-in shortcode is that it only allows for a single condition. If you wanted to display content based on the values from multiple fields, until recently the only solution has been to use multiple shortcodes. At best, it’s a clunky solution, and at worst it’s a headache to troubleshoot.
To solve this problem, we built a plugin that adds support for including multiple conditions in a single shortcode. This will not only save you time, but it gives you the freedom to display content using complex conditional logic in Confirmations, Notifications, and anywhere else shortcodes are supported.
Using the Plugin
Prerequisites
Confirm that you have Gravity Forms installed and activated and that you’ve installed and activated the plugin.
Using the Shortcode
Gravity Forms Advanced Conditional Shortcode adds additional parameters to the [gravityforms]
shortcode to allow for multiple conditions. Let’s step through an example scenario.
Say you run a store that sells accessories for cars and trucks, and you want to offer specific products to your customers on the Confirmation page based on choices they make in the form.
Using Advanced Conditional Shortcodes, you can target specific combinations to display content.
In this example, if a customer selects both “SUV” and “Off-road” they’ll see a message about off-road accessories. Any other combination only displays the default message.
Parameters
Advanced Conditional Shortcodes supports all of the parameters in the vanilla Gravity Forms Conditional Shortcode with the addition of the following.
- relation (string) (required)
The type of relation between the different conditions. Possible relations include:- all
- any
- value (string) (required)
Specify the merge tag to be evaluated by the operator and the compare.
You can find a full list of form-specific merge tags while editing a Notification or Confirmation under the merge tag button: - operator (string) (required)
Enter the operator to be used to test the content of the merge tag with the value you specify. Similar to the condition parameter in the vanilla shortcode, but specific to Advanced Conditionals. Possible conditions include:- isnot
- is
- greater_than
- less_than
- contains
- starts_with
- ends_with
- compare (string) (required)
The value that will be compared against the value based on the operator.
Unlimited value, operator, and compare parameters can be added to the conditional shortcode. Simply add a number to the end of each of the parameters to group them together. For example, a second group looks like this:
value2="{:2}" operator2="is" compare2="Off-road"
Taking It Further
Same as the standard Conditional Shortcode, the Advanced Conditional Shortcode will work in HTML fields when using Gravity Forms Preview Submission. The same is true for using Advanced Conditional Shortcodes in post content via GF Post Content Merge Tags. More info in our Gravity Forms Conditional Merge Tags article.
Hello; i have the following conditional shortcode in my confirmation box;
[gravityforms action="conditional" merge_tag="{:1.3}" condition="isnot" value="{:16}"] [gravityform action="conditional" merge_tag="{:1.6}" condition="isnot" value="{:15}"] [gravityform action="conditional" merge_tag="{:7:d-M-y}" condition="isnot" value="{BvnDoB:17}"]
Credentials Do Not Match! Please ensure your first and last name, as well as your Date of Birth, match with the information provided in your BVN.
[/gravityform][/gravityform][/gravityforms]
I am getting this as the response;
Credentials Do Not Match! Please ensure your first and last name, as well as your Date of Birth, match with the information provided in your BVN. [/gravityform] Invalid Credentials! Your first and last name does not match with the one on your BVN. [/gravityform] [/gravityform] [/gravityform]
Why is it repeating the closing merge tags again? What am i doing wrong please?
Hi Gbenga,
Your shortcode syntax needs some adjustments.
The conditional shortcode is
[gravityforms]
. You have shortcodes that are written as[gravityform]
. Update those to[gravityforms]
and you should be good to go.Hi,
Great code, a real lifesaver. Thank you!
However it seems that you have forgotten to put a
break
here: https://github.com/gravitywiz/snippet-library/blob/6513e423481211b5356156743b77a02ecf69a196/gravity-forms/gw-advanced-conditional-shortcodes.php#L50Regards,
Hi Denes,
Glad the code was useful to you. I will forward your feedback to our developers to look into it. Thanks
Best,
Hi, we are looking to get the title to show in the notifications only if one of fields was selected. It is not working, no title is showing. Thanks
[gravityforms action="conditional" relation="any" value="{:36}" operator="is" compare="Brochure" value2="{:37}" operator2="is" compare2="Pamphlet" value3="{:38}" operator3="is" compare3="Flyer"]This is the title[/gravityforms]
Hi K,
The conditional shortcode you have is correct. The tutorial has a code/plugin to add to your website to get the conditional shortcode to work. Can you confirm if you’ve already added the snippet or installed the plugin on your website?
Best,
Could this be used in a popup to unhide buttons to view webinars on the parent page? I’m looking for something that will work like this page: https://www.on24.com/resources/upcoming-webinars/. Users click the register now buttons to trigger the popup with the form, When the popup closes the page w/the fully playable webinars is accessed. This will be the page: https://www.destinationpet.com/live-webinar/ (password: nehmedia).
Hi Lisa,
I doubt this will work for your use case. Because the player will be on the main page, it will not be possible to use the field values of the form in the popup to set up the conditional shortcode. However, I think our Submit to Access snippet will work for your use case. You can check it out. https://gravitywiz.com/submit-gravity-form-access-content/
Best,
Can this be used to show a form if the logged-in user does not have an entry for the form? We are using the Gravity View plugin to show a user profile UI that links to the Gravity Form entry the user created. If the user does not have an entry, we would like to show the Gravity Form.
Hi Chris,
This is an interesting use case, but I doubt the shortcode would work for that. I’ll forward your request to our product manager. Please expect an additional reply on this soon.
Best,
Replied via a ticket, Chris. 🙏
Can this be used as a Conditional Logic test to compare the value of two fields
so if Field A = Field B display/hide Field C
Hi Ian,
You can use this snippet to compare values, like if Field A = Field B displays Field C, but it works with Conditional shortcodes and not Conditional logic. If you have an active Gravity Perks license, you can get in touch with us via our support form with your account email, so we see how best we can assist you with a solution that will work for the field to field conditional logic.
Best,