Hide elements and widgets conditionally

Estimated reading time: 5 min

One template usually serves many pages. The same product template runs every product, the
same header shows on every screen, one global section sits on hundreds of posts. Sooner or
later a part of it does not belong everywhere: a promo banner that should skip the very
products it advertises, an account block that means nothing to a visitor who is not signed
in, a badge that applies to five products out of two hundred.

Rey adds a Hide for: setting to every Elementor widget, container and section. You pick
a condition, and when it is met the element is simply not printed on that page. The rest of
the template stays exactly as it is, and you do not need a second copy of it.

What you need

Hide for: is part of Rey Core and is always available, there is no module to switch on
first. It works on widgets, containers, old Sections, and everything inside global sections
and templates.

The ACF Field value option is newer, it arrives with Rey 3.4.0. If your dropdown
only offers Logged IN users, Logged OUT users and Custom Script, update the theme and
the Rey Core plugin.

You do not need to buy Advanced Custom Fields for the ACF option, Rey ships ACF PRO built
in and its Custom Fields menu is in the WordPress sidebar by default. If that menu is
missing, the ACF panel was switched off in Rey’s Advanced settings.

Where to find it

  1. Edit the page, the template or the global section with Elementor.
  2. Select the widget, container or section you want to hide.
  3. Open the Advanced tab in the left panel.
  4. Open the Layout section. On old Sections the setting sits under Advanced
    instead.
  5. Set Hide for: to the condition you need.
the Elementor left panel, Advanced tab, with the Hide for: dropdown open
so all five options are readable.

The options

The default is Don’t hide, which means no condition at all. Switching back to it later
leaves your other settings in place, so you can turn a rule off without losing it.

Logged IN users

The element disappears for anyone who is signed in, and stays for everybody else. Use it
for the content that only speaks to strangers: a “Create an account” banner, a first order
discount, a newsletter box you do not want existing customers to see again.

Logged OUT users

The mirror image. The element only exists for signed-in visitors, which suits account
links, order history blocks, wholesale pricing notes or a members-only section.

One caution: if you run a full page cache, make sure it keeps a separate copy for
signed-in visitors, or does not cache their pages at all. Most caching plugins do this
already. If yours does not, both groups can end up looking at the same saved page.

ACF Field value

This one reads a custom field, so the answer can differ for every product, page or term
that shares the template. Tick a field on one product and the element vanishes there, while
every other product keeps it. Four settings appear when you pick this option.

the same panel with ACF Field value selected, showing Select Field,
Or add custom field key, Hide when the field: and Value.

Select Field

The list holds every ACF field on your site, written as Field label (Group name). Start
typing to filter it. Rey’s own internal fields are in there too, which is why the list is
longer than you expect.

The field is read from the page, post or term being displayed at that moment, not from the
page you are editing. That is what makes this useful inside templates: one product
template, a different answer for every product.

Or add custom field key

Leave this empty unless the list cannot give you what you need. It takes a raw field name,
the same syntax as Rey’s ACF dynamic tags, and it is ignored when Select Field is
filled in.

It is also where the {lang} placeholder goes. On a site running Polylang or WPML, a field
name written as heading_{lang} is read as heading_ro for a visitor browsing in Romanian.
When that language’s field was never filled in, Rey falls back to the same field in the
site’s main language, so the rule keeps working while a translation is still missing.

Hide when the field:

This decides what counts as a reason to hide.

  • Has a value hides the element when the field is filled in. This is the default and the
    one you want for a True/False toggle.
  • Is empty hides the element when the field holds nothing. Use it the other way around,
    to show something only on the items that have the field filled in.
  • Equals value hides the element only when the field holds the exact text you type below.
  • Doesn’t equal value hides everything except that exact text.

Value

Only shown for the two Equals rules. Type the exact value to match, without quotes. For a
Select or Radio field, type the field’s value, not its label. When the field holds a list,
such as a Checkbox or a Gallery, a match on any one item counts.

Inside a container with a Dynamic Data Provider

Rey containers have a Dynamic Data Provider section, in the same Advanced tab, which
points a container at another post, product or term. Everything inside it then shows that
content instead of the current page’s.

The Hide for: condition follows the same source. For a widget inside such a container,
the ACF field is read from the post or term the container provides, not from the page the
visitor is on. That is what lets a “featured product” block hide its own parts, for example
a badge that only shows when the featured product has the field ticked.

One limit: a container’s own rule is checked a moment before its provider opens, so it
always reads the current page. If the whole block has to disappear based on the provided
item, put the rule on a container nested inside it instead.

Custom Script

The escape hatch, for conditions the other options cannot express: a number comparison, a
date, an order count, anything you can work out in PHP. You type a short Custom Script ID, then a small snippet with that same ID decides. See the developer section below.

Three things worth knowing

The element stays visible while you edit. The condition is applied on the front end
only, otherwise you could not select an element you had just hidden. Always confirm on the
real page, in a private window when the rule involves being logged in.

Nothing is left behind. The element is not hidden with CSS, it is never printed. That
makes it different from Elementor’s responsive hiding, which still sends the markup and
covers it, and from Rey’s Hide on demand, which shows the element and lets the visitor
dismiss it. Here the markup is absent from the page source, so nothing flashes on screen
and search engines never see it. For a container, the children go with it.

Empty is stricter than it looks. An unticked True/False field counts as empty, which is
what you want. A number field holding 0 counts as a value, because for a price or a stock
count, zero means something. If a 0 is tripping your rule, switch to Equals value and
type the exact value you care about.

Example: hide a promo banner on the products it advertises

You run a “20% off all bags” banner inside the product template, and it should not show on
the bags themselves.

  1. Go to Custom Fields > Field Groups and add a group called Product options.
  2. Add a field labelled Hide promo, of type True/False, with the field name
    hide_promo.
  3. Under Settings, set the location rule to Post Type is equal to Product, then
    publish.
  4. Edit one of the bags. The Hide promo toggle is now on the product edit screen. Turn
    it on and update.
  5. Edit the product template with Elementor and select the banner container.
  6. In Advanced > Layout, set Hide for: to ACF Field value.
  7. In Select Field, pick Hide promo (Product options).
  8. Leave Hide when the field: on Has a value, then update.

Open one bag and one other product on the front end. The banner is gone on the first and
still there on the second. To add another product later you tick one toggle, you do not
touch Elementor.

For developers

Pick Custom Script, set Custom Script ID to low_stock, then return true from the
filter that carries the same ID:

// Hides the element when the product's ACF "stock_alert" number drops under 5.
add_filter( 'reycore/elementor/hide_element/low_stock', function( $hide ){
    return (int) get_field( 'stock_alert', get_the_ID() ) < 5;
} );

The filter also receives the Elementor element as a second argument, if your rule needs to
read the element’s own settings. It runs for every element carrying that ID, on every
request, so keep the check cheap.

Elements with any Hide for: rule are left out of Elementor’s Element Cache on purpose.
A cached copy would freeze the first answer and hand it to everybody.

Add snippets to your child theme’s functions.php or to a snippets plugin, never to the
parent theme, since a theme update would erase them.

Was this article helpful?