Skip to main content
Documentation/Dynamic Data/Conditions/Custom PHP Conditions
Dynamic Data

Custom PHP Conditions

When none of the built-in conditions can express the rule you need, Custom PHP lets
you write it. It's one entry in the condition dropdown like any other, but instead of an
operand and a value it gives you a PHP editor, and your code decides the answer.

It's available everywhere conditions are: on
element display conditions and on template
targeting. It is not available inside the Post Loop's
Query Builder — query conditions are
translated into query arguments, and arbitrary PHP can't be.

Writing one

Add a condition, choose Custom PHP from the Other group, and click Open PHP
Editor
. Oxygen starts you off with a working example:

The PHP Editor dialog for a Custom PHP condition
The PHP Editor dialog for a Custom PHP condition

Your code must return a value Oxygen can read as true or false. Return true and the
condition passes; return false and it fails. There is no operand dropdown on this
condition — you are not comparing anything, you're answering the question yourself.

global $post;
if ($post && has_term('featured', 'post_tag', $post)) {
    return true;
}
return false;

Write it as a plain statement list, not a function: no <?php tag, no wrapping function,
just the body. Anything WordPress has loaded by render time is available.

Where it runs

The code runs at render time, in the same request as the page it affects, with the
WordPress environment fully loaded. That means the usual template globals and conditional
tags work — $post, is_singular(), get_queried_object(), wp_get_current_user() and
the rest.

Whether the builder canvas reflects the condition is up to you: the Conditions tab has an
In-Builder Preview toggle, so you can either see the element hidden as a visitor would
or keep it visible while you work on it.

The security reality

Oxygen evaluates this code directly. It is not sandboxed, not parsed for safety, and not
limited to a subset of PHP — it has exactly the privileges the rest of your site's code
has. Two consequences worth being deliberate about:

  • Anyone who can edit in Oxygen can run PHP on your server. That is the same power a
    theme's functions.php grants, but it's reachable from the builder UI. On a site using
    Custom PHP conditions, treat builder access as equivalent to code access.
  • A fatal error takes the page down. Unlike the PHP Code element, which catches parse
    errors and prints them, a condition's code is evaluated with no error handling around
    it. A syntax error or an uncaught exception doesn't become "condition failed" — it
    surfaces as a PHP error on every page where the condition is evaluated. Test on a page
    you can afford to break.

When to reach for something else

Custom PHP is the escape hatch, and an escape hatch used routinely becomes a maintenance
problem — logic in a condition box is invisible to search, to version control, and to
whoever inherits the site. Before writing one, check that it isn't already covered:

  • The built-in conditions cover post type, status,
    ID, parentage, taxonomy, author, featured image, comment count, user role and login
    state, dates, referrer, browser, and breakpoint.
  • The Dynamic Data condition tests any dynamic field's value with is, is not,
    contains, does not contain, is empty and is not empty — which covers most
    "does this custom field say X" rules without any code.

If you do need PHP and the same rule appears on several elements, consider registering
your own condition instead. Oxygen has an API for it: your condition then appears in the
dropdown by name, with proper operands and values, and the logic lives in a file where it
can be read, searched and version-controlled.