Опис
Block Logic adds a „Block Logic“ field to the „Advanced“ section of the block editor (i.e Gutenberg), that lets you show or hide any block based on conditions. You can use WordPress’ Conditional Tags or any general PHP code.
Show or hide blocks based on
- User role
- User login status
- Post status
- Date and time
- The result of a custom PHP function
Features
- Show or hide any block using conditions
- Combine conditions with “and” or “or” operators. See FAQ Writing Logic Code
- Full flexibility: use any condition you want (you can extend base functionality with filters)
Limitations
Does not work with the Classic Block, Widget Block or Widget Area Block [‘core/freeform’, ‘core/legacy-widget’, ‘core/widget-area’], as the those blocks do not support block attributes. Does also not work with the HTML Block [‘core/html’] inside the Widget Screen, as this one also does not support block attributes there.
Configuration
Just activate the plugin. The „Block Logic“ textbox will then appear in the „Advanced“ section of the Gutenberg editor.
Breaking changes since 2.0.0
Since version 2.0.0 the code does not get eval’d directly, but there is a whitelist of allowed functions that can be extended via a filter.
Upgrade Notice
When upgrading to version 2.x you will need to update your logic. No return is allowed anymore and no semicolons in code as only expressions are evaluated. See FAQ Section Writing Logic Code.
ЧПП
-
Writing Logic Code
-
Make good use of WP’s own conditional tags. Use operators to combine conditions. See Supported functions and globals section for allowed functions. Functions are extendable via filters.
-
Operators
!(not) to reverse the logic, eg!is_home()is TRUE when this is NOT the home page.||(or) to combine conditions.X || Yis TRUE when either X or Y is true.&&(and) to make conditions more specific.X && Yis TRUE when both X and Y are true.
-
WordPress Conditional Checks
is_home()— just the main blog page!is_page('about')— everywhere except this specific WP pageis_user_logged_in()— shown when a user is logged inis_category(array(5,9,10,11))— category page of one of the given category IDsis_single() && in_category('baked-goods')— single post that’s in the “baked-goods” categorycurrent_user_can('administrator')— admin-only blocksstrpos($_SERVER['HTTP_REFERER'], "google.com") !== false— blocks to show when clicked through from Googleis_category() && in_array($cat, get_term_children(5, 'category'))— category page that’s a descendant of category 5in_array(77, get_post_ancestors($post))— WP page that is a child of page 77is_page('home') || $post->post_parent == 13— home page or the page that’s a child of page 13
-
WordPress Post & Term Checks
has_term('special-offer', 'category', $post)— post has a specific category termget_post_meta(get_the_ID(), 'featured', true) == 'yes'— check a meta field valueget_post_type() == 'product'— current post type is a WooCommerce productis_sticky()— sticky posts onlyhas_post_thumbnail()— only posts/pages with a featured imageget_the_category()— fetch post categories (can be used in expressions)get_the_tags()— fetch post tags
-
WooCommerce-Specific Checks
WC()->cart && WC()->cart->get_cart_contents_count() > 0— cart has at least one itemWC()->cart && WC()->cart->total > 50— cart total is more than $50is_product() && get_post_meta(get_the_ID(), '_sale_price', true) !== ''— product has a sale priceis_product_category('shoes') && WC()->cart— category page “shoes” and cart existsis_user_logged_in() && WC()->cart && WC()->cart->get_cart_contents_count() > 0— logged-in user and cart has items
-
ACF Checks (Read-only)
get_field('featured') == true— ACF field “featured” is trueget_sub_field('discount') > 0— ACF subfield “discount” has a valueget_row() > 0— there are ACF repeater rowsget_field_object('product_info')['value'] !== ''— check ACF field object value
-
PHP Helpers
in_array('baked-goods', wp_get_post_categories($post))— category array checkstrpos(get_permalink($post), 'sale') !== false— check if permalink contains “sale”empty(get_post_meta(get_the_ID(), 'stock', true))— post has no stock metacount(WC()->cart->get_cart()) > 1— more than 1 item in carttime() > strtotime('2026-01-01')— check current timestampnumber_format(WC()->cart->total, 2)— format cart totalround(get_post_meta(get_the_ID(), 'rating', true)) > 4— rating is greater than 4
-
-
Supported functions and globals
-
Block Logic comes with a set of safe WordPress, WooCommerce, ACF, and PHP helper functions, as well as selected globals and superglobals. These can be used in block logic expressions to control the display of blocks dynamically.
-
Allowed Superglobals
- By default, only safe, read-only superglobals are available. Developers can extend this list via the
block_logic_allowed_superglobalsfilter.$_GET— query parameters$_POST— submitted form data$_SERVER— server/environment info (e.g.,$_SERVER['HTTP_REFERER'])$_COOKIE— cookies
-
Example usage:
!empty($_GET['ref']) && strpos($_SERVER['HTTP_REFERER'], 'google.com') !== false
- By default, only safe, read-only superglobals are available. Developers can extend this list via the
-
Allowed WordPress Globals
- These global variables are accessible inside block logic expressions. Extend the list via the
block_logic_allowed_globalsfilter.$post— current post object$wp_query— main query object$wp_the_query— backup of main query$wp— WP environment object$wp_rewrite— rewrite rules$current_user— WP_User object of the current user$wp_version— WordPress version
-
Example usage:
in_array($post->ID, get_post_ancestors($post)) && is_user_logged_in()
- These global variables are accessible inside block logic expressions. Extend the list via the
-
Allowed WordPress Functions
- Overview
current_user_can('administrator')— check user capabilitiesget_current_user_id()— current user IDget_option('blogname')— get site titleget_post_meta(get_the_ID(), 'featured', true)— read post metaget_post_type()— post type slugget_the_ID()— current post IDhas_post_thumbnail()— check if post has featured imageis_page('about')— check if current page is „about“
-
Example usage:
is_page('about') && has_post_thumbnail()
- Overview
-
ACF Functions
- Overview
get_field('featured')— read ACF field valueget_sub_field('discount')— read subfield in repeaterget_row()— number of repeater rowsget_field_object('product_info')['value']— field object value
-
Example usage:
get_field('featured') == true && get_sub_field('discount') > 0
- Overview
-
WooCommerce Functions
- Overview
wc_get_cart_contents_count()— number of items in cartwc_get_cart_total()— cart totalwc_get_cart_subtotal()— cart subtotalwc_cart_is_empty()— TRUE if cart is emptywc_customer_get_id()— current customer IDwc_customer_get_billing_email()— customer billing emailwc_customer_get_shipping_country()— shipping countrywc_customer_get_shipping_state()— shipping statewc_customer_get_shipping_postcode()— shipping postcodewc_get_product($product_id)— get WC_Product object
-
Example usage:
wc_get_cart_contents_count() > 0 && wc_get_cart_total() > 50
- Overview
-
PHP Helpers
- Overview
strpos($haystack, $needle)in_array($needle, $array)empty($var)isset($var)count($array)strlen($string)preg_match($pattern, $subject)current_time('timestamp')strtotime($string)date($format, $timestamp)time()number_format($number, $decimals)round($number)
-
Example usage:
count(WC()->cart->get_cart()) > 1 && time() > strtotime('2026-01-01')
- Overview
-
-
Extending the list of allowed functions
-
Block Logic provides four filters that developers can use to safely extend which functions, globals, and superglobals are available inside block logic expressions.
-
Add custom functions
- Use the
block_logic_allowed_functionsfilter to make additional functions available in block logic expressions. -
Example
add_filter('block_logic_allowed_functions', function($allowed) { $allowed[] = 'my_cart_has_items'; return $allowed; }); function my_cart_has_items() { return WC()->cart && WC()->cart->get_cart_contents_count() > 0; } -
Usage in a block logic field:
my_cart_has_items()
- Use the
-
Add extra superglobals
- Use the
block_logic_allowed_superglobalsfilter to allow additional superglobals like $_SESSION. -
Example:
add_filter('block_logic_allowed_superglobals', function($allowed) { $allowed[] = '_SESSION'; return $allowed; }); -
Usage in a block logic field:
!empty($_SESSION['special_offer'])
- Use the
-
Add extra WordPress globals
- Use the
block_logic_allowed_globalsfilter to allow extra global variables. -
Example:
add_filter('block_logic_allowed_globals', function($allowed) { $allowed[] = 'my_global_data'; return $allowed; }); // Somewhere in your theme or plugin $GLOBALS['my_global_data'] = ['foo' => 'bar']; -
Usage in a block logic field:
$post->ID == my_global_data['foo']
- Use the
-
Add completely custom globals
- Use the
block_logic_extra_globalsfilter to allow any additional global variable in block logic expressions. -
Example:
add_filter('block_logic_extra_globals', function($allowed) { $allowed[] = 'my_custom_flag'; return $allowed; }); // Initialize the global somewhere $GLOBALS['my_custom_flag'] = true; -
Usage in a block logic field:
$my_custom_flag && is_user_logged_in()
- Use the
-
Прегледи
Сарадници и градитељи
Block Logic – Full Block Display Control је софтвер отвореног кода. Следећи људи су допринели овом додатку.
СараднициBlock Logic – Full Block Display Control је преведен на 1 језик. Хвала преводиоцима за њихове доприносе.
Преведите Block Logic – Full Block Display Control на свој језик.
Заинтересовани сте за градњу?
Прегледајте код, проверите SVN складиште или се пријавите на белешку градње преко RSS-а.
Белешка о изменама
2.1.5
Raised version to show updated readme.txt
2.1.4
Raised version to show updated readme.txt
2.1.3
Raised version to show updated readme.txt
2.1.2
Raised version to show updated readme.txt
2.1.1
Raised version to show updated readme.txt
2.1.0
- added filters to extend the functionality
- updated README for new plugin version
2.0.0
- totally reworked and removed all directly evalled code
1.0.8
- recompiled assets to remove console.log
1.0.7
- added logic indicator to mark blocks that have logic applied
1.0.6
- added check for Classic Block, Widget Block or Widget Area Block [‘core/freeform’, ‘core/legacy-widget’, ‘core/widget-area’], as those do not support block attributes
- added limitations to plugin description
- updated dev dependencies
1.0.5
- added check for Classic Block and disabled display of settings there
1.0.0
- Initial Release of the plugin

