Show blocks based on custom PHP Logic

This is a premium condition to change the visibility of WordPress content using Conditional Blocks Pro.
(P.S This block is a Reusable Block + Conditions. It won’t be shown if you’ve purchased Conditional Blocks Pro)

Example of adding custom PHP logic to display Gutenberg Blocks

The PHP Logic condition is for power users who require additional control over Gutenberg blocks. Wished you can add your own Condition Types to Conditional Blocks? Well you can! 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 attacked using PHP code like eval.

Allowed functions.

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',
            );

Adding your own Condition Types – Register your functions

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 );

WooCommere Conditions

You can create your own custom WooCommerce conditions as well. We do plan on having dedicated condition types available in Conditional Blocks Pro in the future, though here is an custom & flexible example for implimenting WooCommerce conditions.