This guide contains Custom PHP condition examples for the Pro version of Conditional Content. The examples use APIs available in Joomla 4, 5, and 6 and require PHP 8.2 or newer. Enter the code without <?php tags and end each condition with a boolean return value.

Custom PHP runs on the server with the permissions of your Joomla installation. Only trusted administrators should be allowed to edit it. Filter request values and bind values used in database queries, as shown below.

PHP Condition Rule security

The global PHP Condition Rule restriction is enabled by default under Components > Regular Labs - Conditions > Options > PHP Condition Rules. The allowed group defaults to Super Users.

Only users in an allowed group may create or save a Condition Set containing a PHP Condition Rule. The restriction does not stop saved Condition Sets from being evaluated, so they can be used in modules and other areas without an author. The permission is checked again whenever a set containing PHP is saved.

Conditional Content also supports inline PHP Condition Rules. With the restriction enabled, the PHP control in the editor button is shown only to users in an allowed group. At runtime, inline PHP runs only when it comes from a saved Joomla article whose last editor belongs to an allowed group. If the article has never been edited, its creator is checked.

Inline PHP does not run in modules or other content without a verified article author. Create a reusable Condition Set containing the PHP Condition Rule and select that set in the module or other item. Disabling the global restriction restores the previous unrestricted behavior and should only be done when every user who can enter Conditional Content syntax is trusted to run server-side PHP.

Ready-to-use variables

The condition prepares $app, $document/$doc, $database/$db, $user, and $Itemid. Depending on where the condition runs, $article and $module may also be available.

Article authors

$authorIds = [62, 63, 65];

return isset($article->created_by)
    && in_array((int) $article->created_by, $authorIds, true);

Email domains

$domains = ['@example.com', '@example.org'];
$email = strtolower((string) $user->email);

foreach ($domains as $domain) {
    if (str_ends_with($email, $domain)) {
        return true;
    }
}

return false;

Client IP addresses and trusted proxies

$allowedIps = ['111.111.111.111', '222.222.222.222'];
$clientIp = \RegularLabs\Library\ClientIp::get();

return in_array($clientIp, $allowedIps, true);

The Regular Labs client-IP helper uses the direct peer address from REMOTE_ADDR. It only accepts a forwarded client address when that direct peer matches an address or CIDR range configured under Trusted Proxy Ranges in the System - Regular Labs Library plugin. This prevents visitors from gaining access by sending a forged forwarding header.

Component output text

$search = 'my search string';
$buffer = $document->getBuffer('component');

return is_string($buffer) && str_contains($buffer, $search);

Menu depth

$levels = [2, 3];
$item = $app->getMenu()->getActive();

return $item !== null
    && in_array((int) $item->level, $levels, true);

Custom session counter

$session = $app->getSession();
$key = 'my-condition-counter';
$count = (int) $session->get($key, 0) + 1;
$session->set($key, $count);

return $count <= 10;

This counts how often this condition is evaluated during the session. A condition can be evaluated more than once during a page request, so it is not a page-view counter.

Template parameter values

$parameter = 'colorVariation';
$values = ['blue', 'green'];
$template = $app->getTemplate(true);

return in_array(
    (string) $template->params->get($parameter),
    $values,
    true
);

Reading JSON with Registry

$options = new \Joomla\Registry\Registry(
    '{"layout":"compact","limit":12}'
);

return $options->get('layout') === 'compact'
    && (int) $options->get('limit', 0) === 12;

Filtered request values

$campaign = $app->getInput()->getString('campaign');

return in_array($campaign, ['spring', 'summer'], true);

Choose the getter that matches the expected value, such as getInt(), getString(), or getCmd(). Do not use an unfiltered request value unless you have a specific reason and handle it safely.

Database queries

$authorId = 62;
$minimum = 5;

$query = $db->getQuery(true)
    ->select('COUNT(*)')
    ->from($db->quoteName('#__content'))
    ->where($db->quoteName('created_by') . ' = :authorId')
    ->bind(
        ':authorId',
        $authorId,
        \Joomla\Database\ParameterType::INTEGER
    );

return (int) $db->setQuery($query)->loadResult() >= $minimum;