If statements
Displaying data/content conditionally inside your HTML element and or email body with the build in "if statements" for your WordPress forms
If statements can be used inside your HTML element and inside your email body content. You can also combine it within foreach loops.
What is an email if statement?
With the build in if statements feature you will have more flexibility as to how your email body and or HTML element is generated based on user input in your WordPress form.
In short it allows you to (just like any programming language) construct your own if statements inside your HTML element and or email body content.
Construction of "if statements" explained
Each if statement has a so called operator
to do a so called "Comparison Operation". Inside our WordPress forms (and email body content) we can use one of the following operators for our statements:
==
(equal to)!=
(not equal)>
(greater than)<
(less than)>=
(greater than or equal to)<=
(less than or equal to)??
(contains)!??
(does not contains)
How to create my own if statements?
A basic example of this would be to compare a field value with a hardcoded value, for instance if you want to display some additional information only to users who are named John we could do the following:
When you require to output a default text whenever first_name
is not equal to John we can use the elseif
statement as shown below:
Practical example use cases
Below we will cover some practical use cases that you can apply to your own application(s).
Use case 1: Show extra information based on a selected package:
A simple use case would be to add some text that is specific for a package that a user selected inside your WordPress form. Let's say the package name is called package_1
and it's value when chosen by the user is daily_backups
. When the user selected this package to be included in their order, we would want to display some important information regarding how these backups are being created by the company.
Inside our Confirmation email body (delivered to the user who filled out the WordPress form) we can enter the following if statement:
The above if statement will output the content depending on your own needs whenever the tag {package_1}
is equal to daily_backups
Use case 2: Ask for parental consent when underaged:
Another example could be to check if a user is underaged or not and display some information about needing a parental consent:
The above if statement will display the message only when the user is underaged
Checking if a field exists
When you are using conditional logic in your form, in some cases a field might not be set due to it being conditionally hidden.
In these cases you might want to check if a field exists (is set). You can do so by using the isset()
method. For example:
Last updated