Using the [gvlogic] shortcode
There's a much easier way to achieve the same results demonstrated on this page called "Field Conditional Logic".
Dynamically show or hide content
If you only want to show content if the value matches a specific condition, you can do that with [gvlogic]
.
If you are using GravityView for an Issue Tracker, you may want to display content to a user based on ticket status.
[gvlogic if="{Issue Status:13}" is="Closed"]
You are viewing a closed ticket.
{Assigned Staff:49} was working on your ticket. If you would like to get in touch with them, you can call them at {Assigned Staff Phone:54}.
[/gvlogic]
You can also display alternate content if the value does not match:
[gvlogic if="{Issue Status:13}" is="Closed"]
Your issue is closed.
[else]
I see your issue is {Issue Status:13}. Please email us with any questions.
[/gvlogic]
For the example above, if the "Issue Status" field value is "Closed", users will see "Your issue is closed."
If the value is "Pending", users will see: "I see your issue is Pending. Please email us with any questions."
Shortcode Parameters
The logged_in
Parameter
You can include the logged_in
parameter to combine the logic with whether or nor the current user is logged-in. This requires GravityView 2.3 or newer.
Valid values are:
- "true" or "1" (only show if the user is logged-in)
- "false" or "0" (only show if the user is logged-out)
To display different content depending on whether a user is logged-in:
[gvlogic logged_in="true"]
Hello, logged-in user!
[else]
Be our guest, be our guest! Put our microgravity to the test!
[/gvlogic]
You can use the logged_in
parameters with other comparisons, as described below.
Required Parameter: if
If you are not using the logged_in
parameter, the [gvlogic]
shortcode has one required parameter: if
- it must be set so that the shortcode has something to compare against.
The if
parameter accepts Gravity Forms Merge Tags, available in the Custom Content field.
Comparison Parameters
In the definitions below, the "left value" will be the value of the if
merge tag, and the "right value" is the value passed to the parameter. See "Examples" below for more examples.
is
orequals
- The left and right values are the sameisnot
- The left and right values are differentcontains
- The left value contains the right value, with or without other textnot_contains
- The left value does not contain the right valuestarts_with
- The left value starts with the right valueends_with
- The left value ends with the right value
Number, price, and date comparison
The following parameters can only compare numbers, prices, and dates.
greater_than
- The left value is greater than the right valuegreater_than_or_is
orgreater_than_or_equals
- The left value is greater than or equal to the right valueless_than
- The left value is less than the right valueless_than_or_is
orless_than_or_equals
- The left value is less than or equal to the right value
The else
Parameter
You can define an else
parameter to display if there is not a match, like so:
[gvlogic if="{Number:5}" is="28457" else="That didn't match."]
The field value was 28457.
[/gvlogic]
You can also use Merge Tags in the else
parameter.
[gvlogic if="{Number:5}" is="28457" else="{Number:5} is a bad number."]
The field value was 28457.
[/gvlogic]
In the example above, let's see what the output looks like with different values for the "Number" field (Field ID #5):
28457
: "The field value was 28457"9
: "9 is a bad number."100
: "100 is a bad number."
The [else]
shortcode
Instead of using the else
parameter, you can add [else]
inside the shortcode. This allows you to display different content if the conditions are not met.
[gvlogic if="{Number:5}" is="28457"]
The field value was 28457.
[else]
<h3>You can use HTML inside the shortcode if you want!</h3>
[/gvlogic]
The [else]
shortcode also supports logic! You can use multiple [else]
blocks to check multiple conditions:
[gvlogic if="{Address (City):14.3}" is="New York"]
You live in the Big Apple!
[else if="{Address (State):14.4}" is="New York"]
You live upstate.
[else if="{Address (Country):14.6" is="United States"]
You live in the U.S. of A.
[else]
You live on Earth, but not in the USA.
[/gvlogic]
In the example above, if the address submitted was outside the United States, the displayed content would be "You live on Earth, but not in the USA."
[else if]
supports all of the same parameters as [gvlogic]
, including the different comparisons.
Examples
Use HTML and shortcodes inside the [gvlogic]
shortcode
The only limitation is that you can't use the [gvlogic]
shortcode inside itself. Instead, use the [gvlogic2] shortcode.
[gvlogic if="{Number:5}" is="28457"]
<h1>28457 is my favorite number!</h1>
[else]
<h3>You can use HTML inside the shortcode if you want!</h3>
<p>{Number:5} is not my favorite number. You lose.</p>
[/gvlogic]
</p><h2 id="text-comparison-examples">Text Comparison Examples</h2><h4 id="parameter-is">Parameter: <code>is
You can use [else]
inside the shortcode to display different content if the conditions are not met.
[gvlogic if="{Address (City):14.3}" is="New York"]
You live in the Big Apple!
[else]
You live outside NYC.
[/gvlogic]
Parameter: is
(using the else
parameter)
You can also use the else
shortcode parameter instead of the [else]
block to achieve the same goals. The else
parameter cannot span multiple lines, so it is more limited.
[gvlogic if="{Address (City):14.3}" is="New York" else="You live outside NYC."]
You live in the Big Apple!
[/gvlogic]
Parameter: isnot
[gvlogic if="{Address (City):14.3}" isnot="New York"]
You live outside NYC.
[/gvlogic]
Parameter: contains
Let's say there's a paragraph text field where users submit their favorite quote. You have a special affinity for the All the world's a stage monolog, and you want to share that love.
[gvlogic if="{Paragraph Text:2}" contains="All the world's a stage"]
Whoa, you're quoting Shakespeare's the "All the world's a stage" monolog! It's as I like it!
[else]
Why aren't you quoting "All the world's a stage"? Booo!
[/gvlogic]
Parameter: not_contains
We can flip the contains
example around, as well. This checks if the value does not contain the match.
[gvlogic if="{Paragraph Text:2}" not_contains="All the world's a stage"]
Why aren't you quoting "All the world's a stage"? Booo!
[else]
Whoa, you're quoting Shakespeare's the "All the world's a stage" monolog! It's as I like it!
[/gvlogic]
Parameter: starts_with
Here we check whether the State value starts with "New ".
[gvlogic if="{Address (State / Province):14.4}" starts_with="New "]
You may live in New York, New Jersey, New Hampshire, New Mexico.
[else]
You live in an old state!
[/gvlogic]
Parameter: ends_with
Now we check whether someone's email ends with .rocks
, a very silly top-level domain.
[gvlogic if="{Email:16}" ends_with=".rocks"]
Wooohooo! Your email is rad. 🤘
[/gvlogic]
Matching blank values
You can determine whether a field is empty by checking against an empty value, like so:
[gvlogic if="{Text Field:38}" is=""]
This text field is empty
[/gvlogic]
Similarly, you can check if a field is not empty:
[gvlogic if="{Text Field:38}" isnot=""]
This text field is not empty
[/gvlogic]
Logged-In Examples
In these examples, you will see how you can use the logged_in
parameter to show complex logic to users based on their logged-in status as well as the comparison values.
Here's a simple example to start with:
logged_in="true"
[gvlogic logged_in="true"] The user was logged-in. [else] The user was logged-out. [/gvlogic]
If the user is logged-in in the above example, they see "The user was logged-in.". Otherwise, they see "The user was logged-out.".
logged_in="false"
Let's flip it around with logged_in="false"
[gvlogic logged_in="false"] The user was logged-out. [else] The user was logged-in. [/gvlogic]
This results in the same outcome.
logged_in
combined with other comparisons
[gvlogic if="{Number:5}" is="28457" logged_in="true"] The field value was 28457 AND the user was logged-in. [else] Either the field value was NOT 28457, OR the user was logged-out. [/gvlogic]
Number Comparison Examples
In the examples below, {Number:5}
is the Merge Tag generated by Gravity Forms. :5
is the field ID, not the field value. It's a placeholder that each entry's value will replace.
Parameter: is
[gvlogic if="{Number:5}" is="12"]
The number is twelve.
[else]
The number is not twelve.
[/gvlogic]
Parameter: is
(using the else
parameter)
[gvlogic if="{Number:5}" is="12" else="The number is not twelve."]
The number is twelve.
[/gvlogic]
Parameter: isnot
[gvlogic if="{Number:5}" isnot="12"]
The number is not twelve.
[else]
The number is twelve.
[/gvlogic]
Parameter: greater_than
[gvlogic if="{Number:5}" greater_than="12"]
The number is greater than twelve.
[else]
The number is less than or equal to twelve.
[/gvlogic]
Parameter: greater_than_or_is
[gvlogic if="{Number:5}" greater_than_or_is="12"]
The number is greater than or equal to twelve.
[else]
The number is less than twelve.
[/gvlogic]
Parameter: less_than
[gvlogic if="{Number:5}" less_than="12"]
The number is less than twelve.
[else]
The number is greater than or equal to twelve.
[/gvlogic]
Parameter: less_than_or_is
[gvlogic if="{Number:5}" less_than_or_is="12"]
The number is less than or equal to twelve.
[else]
The number is greater than twelve.
[/gvlogic]
Date Comparison Examples
The [gvlogic] shortcode does not support relative dates; however, field conditional logic does.
Parameter: is
[gvlogic if="{Date:3}" is="2019-01-14"]
The date field value is January 14, 2019.
[else]
You may be relieved to know that the date field value is not January 14, 2019.
[/gvlogic]
Parameter: greater_than
When you want to compare multiple dates, you can do that with greater_than
and less_than
. Here's an example:
[gvlogic if="{Date:3}" greater_than="{Second Date:5}"]
The first Date field is in the future, compared to the Second Date field.
[else]
The first Date field is in the <em>past</em>, compared to the Second Date field.
[/gvlogic]
If you use multiple date formats, you may want to add :mdy
Date Field Merge Tag Modifier to the end of each merge tag to force a standardized format.
Here's how the previous example looks when forcing a common format:
[gvlogic if="{Date:3:mdy}" greater_than="{Second Date:5:mdy}"]
The first Date field is in the future, compared to the Second Date field.
[else]
The first Date field is in the <em>past</em>, compared to the Second Date field.
[/gvlogic]
View Context Examples
Display content based on the current View mode ("context"), which corresponds to the View layout tabs when editing an entry: Multiple Entries, Single Entry, and Edit Entry.
Available context
values:
multiple
- Multiple entries are visiblesingle
- Viewing a single entryedit
- Editing a single entrysingular
- Is eithersingle
oredit
context
Parameter: is
[gvlogic if="context" is="multiple"]
Click the name of the person you want to learn more about!
[/gvlogic]
Parameter: is
(using the else
parameter)
[gvlogic if="context" is="single" else="You are viewing multiple entries."]
You are viewing a single entry.
[/gvlogic]
Parameter: isnot
[gvlogic if="context" isnot="singular"]
You are viewing multiple entries.
[else]
You are viewing or editing a single entry.
[/gvlogic]
Advanced And/Or using ||
and &&
ℹ️ Whitespace is not stripped. There should be no spaces between the values being compared and the &&
or ||
characters.
Using ||
for OR statements
Sometimes you may want to compare a value against multiple other values, like if an email address ends with different domains. For example, Apple iCloud accounts can end with @icloud.com
or @me.com
or @mac.com
. They all point to the same email address. The same issue exists with Google and Yahoo…they have multiple domain aliases.
How can we accomplish that with [gvlogic]
? By using the ||
characters to represent "OR":
[gvlogic if="{Email:16}" ends_with="@icloud.com||@me.com||@mac.com"]
Your email address is from Apple.
[else if="{Email:16}" ends_with="@gmail.com||@googlemail.com"]
Your email address is from Google.
[else if="{Email:16}" ends_with="@ymail.com||@yahoo.com"]
Your email address is from Yahoo.
[else]
Your email is not from Apple, Google, or Yahoo.
[/gvlogic]
You can also use Merge Tags in the comparison. This code will check whether Field #4 is greater than Field #5 OR Field #6:
[gvlogic if="{Number:4}" greater_than="{Another Number:5}||{Another Number:6}"]
{Number:4} is bigger than Field #5 OR Field #6.
[else]
{Number:4} is less than or (equal to) both Field #5 and Field #6.
[/gvlogic]
These examples are true:
[gvlogic if="4" equals="3||4||5"]
is true because 4 equals one of the values "3" or "4" or "5".[gvlogic if="carbon" contains="car||bon"]
is true because "carbon" contains one of the values "car" or "bon".[gvlogic if="carbon" starts_with="b||car"]
is true because "carbon" starts with one of the values "b" or "car".[gvlogic if="carbon" ends_with="a||bon"]
is true because "carbon" ends with one of the values "a" or "bon".
These examples are false:
[gvlogic if="4" equals="asd||feigegieng"]
is false because "4" does not equal "asd" or "feigegieng"[gvlogic if="carbon" contains="donkey||egg custard"]
is false because "carbon" does not contain "donkey" or "egg custard"[gvlogic if="carbon" ends_with="lion||flower"]
is false because "carbon" does not end with "lion" or "flower"[gvlogic if="4" isnot="4||5||6"]
is false because "4" is one of the values "4", "5", or "6" and the comparison used is "isnot"
Using &&
for AND statements
When you want to test whether a value matches multiple statements at the same time, you can do that using the &&
characters to ensure that all the values being checked match the test.
Pretend that you have a job application form and want to display different content depending on whether the applicant used certain words in their submission. You asked them to mention Google Docs, Photoshop, and Excel in the field labeled "Skills". If they didn’t, they didn't meet the application criteria.
[gvlogic if="{Skills:2}" contains="Google Docs&&Photoshop&&Excel"]
✅ The applicant correctly referenced the skills necessary in their writing sample.
[else]
⛔️ The applicant failed to reference the correct skills.
[/gvlogic]
These examples are true:
[gvlogic if="carbon" contains="car&&bon"]
is true because "carbon" contains "car" and "bon"[gvlogic if="4" greater_than="1&&2"]
is true because "4" is greater than "1" and is greater than "2"[gvlogic if="4" greater_than_or_equals="1&&2&&3&&4"]
is true because "4" is greater than or equal to "1", "2", "3" and "4"
These examples are false:
[gvlogic if="4" isnot="4&&5&&6"]
is false because "4" is equal to "4" and the comparison used is "isnot"[gvlogic if="4" greater_than_or_is="4&&5"]
is false because "4" is not greater than or equal to "5"[gvlogic if="carbon" contains="car&&island"]
is false because the word "carbon" does not contain both "car" and "island"