You can use Conditional Blocks with custom PHP logic to control the visibility of WordPress blocks.
Conditions API
We have new native API for adding fully custom conditions (including custom fields) within Condition Builder.
Depending on your case you should consider using the integration, instead of the PHP Logic condition.
The PHP logic condition is one of the most powerful features in Conditional Blocks. You can use it to create any custom visibility condition you can think of and integrate any plugin with Conditional Blocks.
Note: We recommend this condition for developer-level use. Conditional Blocks does not run the PHP directly due to security reasons. The functions will be called with the parameters using call_user_func_array
The PHP Logic condition is for power users who require additional control over WordPress blocks. You can use any of the pre-allowed functions or add your own using the example code snippet below.
This condition is secure and locked down to prevent potential scripting attacks using PHP code like eval
.
How does PHP Logic work?
Conditional Blocks will attempt to securely run the functions you’ve set inside the PHP logic condition for the selected WordPress block.
We do not want people to accidentally run harmful PHP functions when WordPress blocks are rendered on the frontend. Therefore, only the registered (allowed) functions will be run by Conditional Blocks.
A PHP logic function should be treated as a conditional check – That means each function MUST return TRUE
or FALSE
. For example, for a block to be visible, the function MUST return TRUE.
Allowed functions.
We have allowed a few functions by default. Each of these functions can be freely used within PHP Logic conditions. They are already registered and safe to use.
$allowed_functions = array(
'is_single',
'has_term',
'in_category',
'is_category',
'get_option',
'has_tag',
'is_tag',
'is_tax',
'is_page',
'get_post_meta',
'has_post_thumbnail',
'has_excerpt',
'is_sticky',
);
Code language: PHP (php)
Using PHP Logic functions
Let’s say you have a WordPress Image block that you want to be visible if the post doesn’t have a featured image. This could be a good use case when WordPress Full Site Editing is widely available.
Example (with NOT operator)
We’ll use one of the above functions that are already registered.
has_post_thumbnail
is an official WordPress function. It will return TRUE if the post has a thumbnail aka feature image. If the post doesn’t have a thumbnail, then the function will return FALSE.
The PHP Logic function requires functions to be TRUE to make blocks visible.
Therefore we’ll need to use the NOT Logical Operator in front of the function. The symbol for NOT is a !
!has_post_thumbnail
is TRUE if the post doesn’t have a thumbnail.
- You’ll first create the image block
- Open the Condition builder and add the PHP Logic condition.
- Configure the PHP logic condition by writing
!has_post_thumbnail
- Save and view the page.
When you view the page the image block will now be displayed if the current post doesn’t have a featured image set.
Example (Parameters)
Function parameters can be added to further configure visibility for desired results.
has_post_thumbnail
will check if the current post has a thumbnail. But we can make it check if another post has a thumbnail by using a Post ID.
has_post_thumbnail(100)
will check if the Post with ID 100 has a thumbnail or not.
Function parameters become powerful when you combine them with your custom functions. We recommend that you look at our WooCommerce code sample.
Creating a custom function
You will need to register your own custom or third-party functions before you can use them with Conditional Blocks. Have a look at the below example.
/**
* My newly created function to determine if blocks should show.
*
* @return boolean true/false to show the block or not.
*/
function my_custom_function() {
// .. add your logic here, even third-party integrations.
return true;
}
/**
* Add custom functions to be used with PHP Logic conditions.
*
* @param array $allowed_functions
* @return array $allowed_functions
*/
function custom_add_allowed_function_conditional_blocks( $allowed_functions ) {
array_push( $allowed_functions, 'my_custom_function' );
return $allowed_functions;
}
add_filter( 'conditional_blocks_filter_php_logic_functions', 'custom_add_allowed_function_conditional_blocks', 10, 1 );
Code language: PHP (php)
Sample – WooCommerce function
We’ve created a sample WooCommerce function on Github to use with the PHP logic condition in Conditional Blocks Pro.
The sample registers the custom function called custom_has_product_category_id_in_cart
, so it can be applied to WordPress blocks using the PHP Logic condition.
You can also see how the function takes a parameter (a product category ID) and determines whether an item in the customer cart has that category.
If a product in the “Hoodie” category is found in the customer cart, then our WordPress Block will be visible.
The sky is the limit! Send us your cool custom functions and integrations. We’d love to create a snippet library for everyone to share.