Menu
All plugins

Inline Conditions for Bricks

Show different text from one Bricks element. Write an inline if, elseif and else in any field that accepts dynamic data, instead of duplicating elements and hiding them with conditions.

{if:{places}<=3:Only {places} places left|Places available}

There are no settings, scripts or styles. The plugin does nothing until a field contains {if:...}, and it works with every dynamic data tag, including ACF, custom fields, query loop values and tags added by other plugins.

Version: 0.1.0 · Requires: Bricks 1.9, WordPress 6.5, PHP 8.1 · See the live demo

Installation

  1. Upload and activate the plugin. Bricks must be active.
  2. Type a condition into any Bricks field that takes dynamic data, such as a heading, text, button or link.
  3. There is nothing to switch on.

Syntax

{if:CONDITION:THEN}
{if:CONDITION:THEN|ELSE}
{if:CONDITION:THEN|elseif:CONDITION:THEN|ELSE}
  • CONDITION is tested first. When it is true the THEN text is shown, otherwise the next elseif is tried, and finally the ELSE text.
  • THEN and ELSE can contain other dynamic data tags and even other {if:...} tags. Text and tags are resolved by Bricks as normal.
  • Spaces around : and | are ignored. To keep a space at the edge of a branch, put it outside the tag or use &nbsp;.

Comparing values

Write Meaning
{x} == 5, {x} != 5 Equal, not equal. Numbers compare as numbers, text ignores case.
{x} === 'Abc', {x} !== 'Abc' Exact match, case sensitive.
{x} > 5, {x} >= 5, {x} < 5, {x} <= 5 Greater or less. Numbers compare as numbers, anything else is compared as natural text, so ISO dates work.
{x} contains foo Contains text, ignoring case.
{x} not contains foo Does not contain it.
{x} starts with foo, {x} ends with foo Start or end of the text.
{x} in a, b, c Matches any item in a comma separated list. not in is the opposite.
{x} is empty, {x} is not empty Nothing there, or something there.
{x} on its own True when there is a value that is not 0, false, no, off or null.

Numbers understand thousands commas and a leading currency symbol, so £1,250.50 is greater than 1000.

Combining conditions

Use and, or and not, with brackets to group. and binds tighter than or.

{if:{places}>0 and {type} contains safari:Book now|Enquire}
{if:not {sold_out}:Available|Sold out}
{if:({a}>5 or {b}>5) and {c} is not empty:Yes|No}

Several outcomes

{if:{places}<=0:Fully booked|elseif:{places}<=3:Only {places} left|Places available}

Values with spaces, colons or quotes

A value with spaces can be written as it is ({name} == Pallas Cat). Put it in single or double quotes when it contains a colon, a bracket, a word that is also an operator (and, or, not, in and so on), or starts or ends with a space.

{if:{time}=='10:30':Morning departure|Later departure}

Escape a literal | in your text as \|, and a literal } or { as \} or \{.

Where it works

Any Bricks field that renders dynamic data: headings, text, buttons, links, image alt text, element attributes, custom fields and query loops. Inside a query loop each item is evaluated on its own.

When something is wrong

A tag that cannot be understood shows nothing to visitors. Anyone who can edit posts sees a hidden HTML comment in the page source that says what is wrong, for example missing ":" between the condition and its text. A tag with no closing } is left exactly as written.

A dynamic data tag that Bricks cannot resolve counts as empty. Output that looks like {echo:...} is always treated as empty, so a condition can never run code.

For developers

  • icfb/enabled (filter, default true, receives the context): return false to skip evaluation for a context you do not want.
  • Evaluation is capped at 200 tags per field and 8 levels of nesting.
  • Run the tests with php tests/run-tests.php.

Known limits

  • A condition cannot run PHP or call functions.

Shortcodes in a condition

A shortcode typed into a condition is run and its output is used as the value to test.

{if:[stock id=5] > 0:In stock|Sold out}
{if:[stock id=5] > {places}:Plenty|Limited}
  • Only shortcodes you type into the condition run. A value that a dynamic data tag returns is never run as a shortcode, so a visitor cannot make your site run one by putting it in a URL or a form field.
  • The output has its HTML removed and any curly brackets taken out before it is compared.
  • A name that is not a registered shortcode is left as plain text. Inside quotes a shortcode is plain text too.
  • Up to 20 shortcodes run per field.

Shortcodes in the text you show

The text after the colon, and the else text, can be shortcodes too. Only the branch that wins is used, so a shortcode in a branch that does not match never runs.

{if:{acf_places}==0:[waiting_list_button]|[booking_form_button]}
{if:{acf_places}==0:[waiting_list_button]|[booking_form_button event="{post_id}"]}
  • Dynamic data tags in the winning text are resolved first, so a shortcode can use them as attributes, as in event="{post_id}".
  • The values those tags return can never run as shortcodes. Their square brackets are neutralised.
  • Text with no registered shortcode is left exactly as it was and Bricks handles it as normal.
  • Return false from the icfb/shortcodes filter to turn off shortcodes in both conditions and shown text.

The wizard

You do not have to remember the syntax. In the Bricks builder, click into any text field in the settings panel and a small if button appears under it. Press it and the wizard opens as a floating window, like Bricks Inspector.

  • Field with a condition: the wizard reads the condition already in the field into its steps, so you edit it instead of starting again. Update the field replaces just that condition and leaves the rest of the text alone. The button is highlighted on fields that already hold a condition.
  • Field with several conditions: it loads the one at your cursor, or the first.
  • Empty field: it opens blank, and Insert into field writes the condition at your cursor.
  • A condition it cannot read into steps (one that uses brackets, not, or a mix of and and or): the wizard tells you, and inserting replaces it.

A live preview shows what visitors would get for sample values you type. Copy works anywhere.

Put the wizard on any page with the shortcode [icfb_wizard]. Its script and styles load only in the builder and on pages with that shortcode. Try the wizard.