How to Develop Custom Rules For the Conditional Display Option
19min
Thrive Architect gives you the possibility to register your own custom conditional display rules. This article will show you how you can create them.
Please note that creating custom rules is an action that should be performed by developers or more tech-savvy persons, as it does require technical knowledge.
Example 1: You can add a new field, called "Page - Demo" with a subfield called "Title - Demo". It will also add a field called "Number of comments - Demo" to the already existing "User" entity.
After activating the plugin, the new fields will be visible in the conditional display pop-up of Thrive Architect:
Note: You can find this demo example on GitHub, here.
Example 2: we'll show how to create a new rule that is conditioned by whether the user has WP Fusion tags assigned to them or not.
Here are the steps to be taken in order to create a new rule:
→ Define the entity class
Firstly, an entity class will have to be defined. This should describe the subject/entity, as well as its properties.
The create_object property, upon running the condition logic, will create the entity object containing all the data representing individual fields.
PHP
|
<?php
publicfunctioncreate_object($param){
$user_id =get_current_user_id();returnget_userdata( $user_id );}/**
* Determines the display order in the modal entity select
*
* @return int
*/publicstaticfunctionget_display_order(){return0;}
→ Register entity class using the tve_register_condition_entity function
As a result, once you put everything together, the code snippet will look like this:
PHP
|
<?php
classUserextends\TCB\ConditionalDisplay\Entity{/**
* @return string
*/publicstaticfunctionget_key(){return'user_data';}publicstaticfunctionget_label(){returnesc_html__('User','thrive-cb');}publicfunctioncreate_object($param){
$user_id =get_current_user_id();returnget_userdata( $user_id );}/**
* Determines the display order in the modal entity select
*
* @return int
*/publicstaticfunctionget_display_order(){return0;}
→ Create an entity field that will be used to fetch the specific entity data which the condition will be tested on
Example (in the visual editor):
→ Extend the field class and define specific properties to describe your field
The get_conditions type can be one of the registered types of conditions that extend the condition class, or you can create your own and register it, using the tve_register_condition function:
PHP
|
<?php
/**
* Register a new condition
*
* @param \TCB\ConditionalDisplay\Condition\string $condition
*/functiontve_register_condition($condition){TCB\ConditionalDisplay\Condition::register( $condition 0;}
Use the condition class as a parameter.
The get_value property has to be defined. This will fetch the value from the entity upon running the condition logic.
→ Register the field class using the tve_register_condition_field function
PHP
|
<?php
/**
*Register a new condition field
*
*@param TCB\ConditionalDisplay\Field|string $field
*/functiontve_register_condition_field($field){TCB\ConditionalDisplay\Field::register( $field );}