Field Editor Conditional Shortcodes 1.7.0 – 1.8.0

As of version 1.7.0+ the shortcodes now have conditional logic integrated with them.  For any specific examples below, I will be using [custom_field] shortcode, but they all will work the same.  See the end of this page, for video tutorial that goes over these features.

This documentation is for Field Editor 1.8.0 or OLDER, please see new documentation for 1.8.1 or newer!

Full shortcode attributes and reference can be found here:

Nesting Caveats (version 1.8.0 or older)

If you want to use shortcode conditionals, for the time being, there is one caveat that you need to be aware of.  You can not use the same shortcode nested inside of a conditional shortcode.  This has been fixed in version 1.8.1 or newer by adding additional shortcodes you can use, as demonstrated above. As an example, this will NOT work:

The reason the above code WILL NOT WORK CORRECTLY is because WordPress parser can not parse shortcodes that are used as non-enclosing and enclosing shortcodes.  Basically this means that the parser will assume the “content” is everything below [custom_field key="job_location"]  and above [/custom_field]  … instead of our intended everything below [custom_field key="job_salary"]  and above [/custom_field]   … as you can assume, WordPress has no way to know which opening shortcode is supposed to go with the closing one.

Temporary Workaround

The temporary workaround for the caveat above, is to ONLY use one of the available shortcodes as your enclosing shortcode, and one for outputting field values.  Because [custom_field]  [job_field]  and [resume_field]  all work exactly the same, this is fairly easy to deal with for the time being.  My suggestion is to use the [custom_field] shortcode for any enclosing shortcodes, and use [job_field] or [resume_field] for value outputs.  To show you what I mean, here is a WORKING revision on the code from above (that does not work):

Notice how we no longer are mixing open/closing shortcodes, and non-closing shortcodes.  In the example above I decided to use the [custom_field] shortcode for shortcode conditionals, and [job_field] for outputting field values.  If you’re using shortcodes in say, a widget, you will only need to do this if you are outputting multiple fields in the same widget, as each widget is parsed individually for shortcodes.

THIS is not an issue in 1.8.1 or newer, please upgrade to the latest release and follow the updated documentation page:

What are conditionals?

Conditionals allow you to specify when you want some custom content to be output, based on the value of a field on a listing.  The best way to explain this, is with examples, so let’s dive right into it.  Here’s a basic example of using the conditional shortcodes:

As you can see above, instead of using a standard [custom_field key="job_salary"]  you can now add a closing shortcode ( [/custom_field] ) which allows you to specify custom content (or other shortcodes), before the closing shortcode, which will only be output, if that field has a value.

In the example above, if job_salary had a value of $40,000, this would be the output on the listing:

Conditional [else]

In additional to the standard conditional shortcode, you can also nest an [else]  shortcode inside of the shortcode content, to specify content to output if the field does not have a value.  Using the example above, let’s add an [else] shortcode to it:

In the example above, we have added an [else] shortcode, which will output any content (and/or shortcodes) below the [else] shortcode, and above the closing shortcode [/custom_field] , when the field does not have a value (or the original logic evaluates to false.  See below for more details).

Conditional Value Checking

You can also fine tune the shortcodes even more, using conditional value checking.  Conditional value checking allows you to add any of the available value checks below, to the shortcode attributes:

  • if_contains  – single string non-taxonomy value fields – check anywhere in the string value of the field, if it contains the passed value
  • if_equals  – single string non-taxonomy value fields – check if the string value of the field, equals exactly the value passed
  • has_value  – array/multiple/taxonomy value fields – check if any of the field values, equals exactly the value passed
  • has_value_containing  – array/multiple/taxonomy value fields – check if any of the field values, contains any part of the passed value

So let’s get right into an example for the attributes above.  Let’s say you have a field with a meta key of job_start_time, and you want to output specific text if that value contains AM or PM:

This would check the value of the job_start_time field, and if it contains AM in the value, it would output whatever is inside the shortcode content (before closing bracket).  To keep the examples simple, I removed the [else]  shortcode, but that can be used anywhere in the conditional shortcodes logic.  If the value of that field does not contain AM (exactly as you entered it), nothing will be output.

Array/Multiple/Taxonomy Value Fields

These type of fields would be something like, multiselect fields (including taxonomies), or multiple file upload fields.  If the field type can save multiple values you must use  has_value  or has_value_containing  … which work exactly the same as if_contains  and if_equals … but for multiple value fields.  Example using job_category when set to allow multiple categories:

Something to keep in mind though … if the user selected Restaurant AND Cafe under categories, the above will output specifically because Restaurant was selected .. even if other categories were selected as well!

Taxonomy Fields

If the field you are outputting a value from is a taxonomy field, you MUST use the has_value or has_value_containing attribute (instead of if_equals or if_contains), even if it’s not a multiselect or multi-value taxonomy field.  This is because Taxonomy fields are returned as an array (even if they are single value/selection), and as such, you must use the attributes for array/multi fields even if they are single dropdown taxonomy select fields.

Negating Shortcodes Conditions

You can also negate conditional value checking, by adding the NOT attribute (without a value) inside the opening shortcode.  This basically will do the opposite of whatever value checking attribute you decide to use.  So using our example above, if I wanted to output something (without using an [else] statement), when job_start_time does NOT contain “AM”:

Using a negated shortcode condition would be used in situations where you don’t want to output anything when it equates to true, but do want to output something with it does NOT equate to true.  This would be exactly the same as doing this:

Case Sensitive Values

There may be situations where you are unsure if the user entered AM or am (uppercase or lowercase, or anything in between), and by default the values you enter ARE NOT CASE SENSITIVE … if you want them to be case sensitive, you can also pass the case_sensitive attribute with a value of true and it will check values, only matching if the value is exactly the same (case wise):

Video Tutorial

The conditional shortcodes starts around 8 minutes in, on the video below:

Total 0 Votes

Tell us how can we improve this post?

+ = Verify Human or Spambot ?