Add your own Rule Check Filter to Dynamic rules
How do you feel about the Power of hooks and filters in WordPress?
Although we have vast dynamic rule conditions that cover almost all scenarios. We sometimes have some exceptional cases for which we need to customize our rules. Thus, we introduced this Rule check Filter. In this filter, you can check your conditions; you need to return true and false from your function, which will show/hide the section/widget based on your logic.
Now you can add more complex filters to the Dynamic rules through your code that returns true/false and, based on the result, can apply the dynamic rule.
Here, we will take an example case to show/hide a section based on a date range. For example, we will offer a discount coupon banner till some particular date. It will not be possible in our existing dynamic rules condition. So, we will use the custom rule filter to check.
Assumptions
The following article assumes that
- You must know the basic functionality of the Dynamic Rules and how it works.
- You must know how the filters work in WordPress. Read more about WP add_filter
Steps to add your own filter to the Dynamic Rule
- Under the "Dynamic Rule" section, check for the option "Rule Check Filter."
- Add your custom filter name in that field. In this case, we have placed "check_post_date."
- We will need to attach the filter to this function in functions.php of the active child theme.
See the following code that we have added to fetch the post date.
add_filter('aepro/dynamic_rules/check_rule/check_post_date', function($check){ global $post; $date = strtotime('22-10-2022'); $post_date = strtotime(date('d-m-Y', strtotime($post->post_date))); if($post_date < $date){ $check = true; }else{ $check = false; } return $check; })
- As you notice in the above code, we have added a filter, "check_post_date," in our function file. This filter will return a true or false value.
- We created a discount coupon banner section that will only show until 22-10-2022.
- Here we are comparing Date. If today's date is before 22-10-22, it returns true, and the section will show. After that, the section will hide automatically.