Advanced

Create a Custom Rule

Learn how to create a Custom Access Condition Rule.

Follow the bellow steps to create your custom rule, you can alternatively use this rule example in GitHub which includes more details and a working code. Also notice that core rules are a useful resource to get started and can be found in the modules/condition/rules directory relative to the installation folder.

Extending YOOtheme Pro

The following guide assumes you are already familar with code, PHP, and extending YOOtheme Pro.


1. Create the Rule Class

Start by creating a PHP Class that extends ZOOlanders\YOOessentials\Access\AccessRule and declares a resolve function. Store it into a YOOtheme Pro Child Theme or a Module.

use ZOOlanders\YOOessentials\Access\AccessRule;

class MyCustomRule extends AccessRule
{
    /**
     * @param \stdClass $props The settings values from the rule fields
     * @param \stdClass $node The current element node being evaluated
     */
    public function resolve($props, $node) : bool
    {
      // return early if no configuration is set
      if (!isset($props->foo)) {
        return true;
      }

      // evaluate...
      $result = false;

      // return a boolean indicating the validation status
      return $result;
    }

2. Create the Rule Config

Create a config file in JSON format that will specify the rule configuration, store it beside the PHP class as config.json.

{
    "name": "",
    "title": "",
    "icon": "",
    "description": "",
    "group": "",
    "collection": "",
    "fields": {}
}
PropDescriptionRequired
NameThe rule name prefixed by yooessentials_access_, e.g. yooessentials_access_myrule.
TitleThe rule title as should appear in the UI, e.g. My Rule.
IconThe absolute path to the rule icon.
FieldsThe rule fields configuration. Those will hold the values of the configuration and are based on the same workflow as the Elements Fields.
DescriptionThe rule description explaining what it validates.
GroupRules with the same group will be displayed under the same section in the UI.
CollectionRules of the same collection will be displayed joined in the UI, indicating a strong relation between them.

3. Declare the Rule

Declare custom rules by adding a yooessentials-condition-rules key to the config.php or bootstrap.php file referencing the rule class and a path to it config.

<?php

require_once __DIR__ . '/rules/MyCustomRule/MyCustomRule.php';
require_once __DIR__ . '/rules/MyOtherCustomRule/MyOtherCustomRule.php';

return [

    'yooessentials-condition-rules' => [
        MyCustomRule::class => __DIR__ . '/rules/MyCustomRule/config.json',
        MyOtherCustomRule::class => __DIR__ . '/rules/MyOtherCustomRule/config.json',
    ]

];
Previous
Season