Conditional logic: run code exactly where it belongs

Role, page type, URL, date and more. Build grouped AND/OR rules visually, without writing the checks yourself.

Two condition groups in the FluentSnippets editor. The first requires post type includes product AND logged-in equals False. An OR divider separates it from a second group requiring URL includes /checkout.
Conditions inside a group are ANDed. Groups are ORed.

Most custom code does not belong everywhere. A free-shipping bar belongs on shop pages. A tracking pixel belongs on the thank-you page. An admin notice belongs to one role.

The usual way to express that is to wrap the code in an if:

if ( is_singular( 'product' ) && ! is_user_logged_in() ) {
    // the actual snippet
}

That works, and there is nothing wrong with it. But it puts the where and the what in the same place, and after a dozen snippets you are reading conditionals to find out which page a snippet affects. FluentSnippets pulls the where out into a rule you build in the editor, so the code stays just the code.

Open a snippet, expand Advanced Conditional Logic, and switch it on.

Groups are the whole model

There are only two rules to learn:

  • Conditions inside one group must all match. They are ANDed.
  • Any one group matching is enough. Groups are ORed.

So the rule in the screenshot above reads: run this snippet when (the post type is product AND the visitor is logged out) OR (the URL contains /checkout).

Use + And to add a condition to the current group, and the OR divider to start a new one. The bin icon removes a condition; removing the last one in a group removes the group. A snippet with conditional logic switched off runs everywhere its run location allows.

What you can match on

Sources are grouped into User, Page and Date — plus FluentCRM when it is installed.

SourceConditions
UserLogged-in, User Role
PageType of page, Post Type, Taxonomy Page, Taxonomy Term Page, URL, Single Post/Page/CPT
DateDate Range, Time Range, Day of the week
FluentCRMIs a CRM Contact, Contact Tags, Contact Lists

A few behaviours are worth knowing before you build a rule that quietly never matches:

  • Type of page returns one value per request, checked in order: homepage, singular, archive, search, 404, author. A single post is is_singular, never is_archive.
  • Post Type and Single Post/Page/CPT only match on singular requests. On a post type archive they are false, even though the archive is of that post type. Use Type of page → Archive for that.
  • URL matches the full current URL including the query string, lowercased. includes with /pricing matches both /pricing/ and /pricing/enterprise/.
  • User Role matches against every role the current user holds, so a user with two roles matches either one. A logged-out visitor has no roles at all, which makes not includes in Administrator true for them — usually what you want, occasionally a surprise.
  • Date conditions use the site’s timezone, not the visitor’s.

Three rules worth stealing

A seasonal banner that switches itself off. One group: Date Range within 1 Dec – 26 Dec. No follow-up task, no reminder to disable it in January.

An office-hours notice. Two conditions in one group: Time Range within 09:00 – 17:00 AND Day of the week includes in Mon–Fri.

A conversion pixel on the thank-you page only. One condition: URL includes /order-received. Cheaper and clearer than a plugin that fires on every page and checks at runtime.

The one behaviour change to remember

Conditions need to know which page is being served, and that is not known at the very start of a request. So enabling conditional logic on a Functions snippet moves it from the setup_theme hook to the wp hook, which is the first point at which the main query has run.

That is considerably later. If your snippet registers a post type, adds an image size, or does anything else that must happen early, conditional logic will break it — not because the rule is wrong, but because the code now runs after the moment it needed to.

Content, Styles and Scripts snippets are unaffected: they are already attached to output hooks that fire well after the query. Shortcode snippets are evaluated when the shortcode renders.

TIP

Conditions are cheap, but they are not free — a snippet with conditions is still loaded and evaluated on every request. If a snippet applies to one page, conditional logic is the right tool. If it applies to nothing at the moment, set it to draft instead.

Where the rule lives

The rule is written into the snippet’s own file, as a JSON value on the @condition line of its header:

<?php
/*
 * @name: Free shipping bar
 * @type: php_content
 * @run_at: wp_body_open
 * @condition: {"status":"yes","type":"and","items":[[{"source":"post_type","operator":"includes","value":["product"]}]]}
 */

Which means it travels with the snippet. Export it, commit it, copy the file to another site — the rule goes with it, because there is nothing else holding it.

The full reference, every operator and every source, is in the conditional logic docs.

Enjoyed this? Get the next one by email.

New articles, practical snippets and feature walkthroughs — sent when there's something worth reading, not on a schedule.

No spam, no selling. Unsubscribe anytime.

Custom code, without the database.

FluentSnippets is free on WordPress.org. Zero queries, no lock-in.