Title: Block Logic &#8211; Full Block Display Control
Author: landwire
Published: <strong>2020 年 1 月 15 日</strong>
Last modified: 2026 年 5 月 30 日

---

搜尋外掛

![](https://ps.w.org/block-logic/assets/banner-772x250.png?rev=2227643)

![](https://ps.w.org/block-logic/assets/icon.svg?rev=2227643)

# Block Logic – Full Block Display Control

 由 [landwire](https://profiles.wordpress.org/landwire/) 開發

[下載](https://downloads.wordpress.org/plugin/block-logic.2.1.6.zip)

[即時預覽](https://tw.wordpress.org/plugins/block-logic/?preview=1)

 * [詳細資料](https://tw.wordpress.org/plugins/block-logic/#description)
 * [使用者評論](https://tw.wordpress.org/plugins/block-logic/#reviews)
 * [開發資訊](https://tw.wordpress.org/plugins/block-logic/#developers)

 [技術支援](https://wordpress.org/support/plugin/block-logic/)

## 外掛說明

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](https://codex.wordpress.org/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**.

## 螢幕擷圖

[⌊The 'Block logic' field at work in the block editor.⌉⌊The 'Block logic' field 
at work in the block editor.⌉[

The ‘Block logic’ field at work in the block editor.

## 常見問題集

### Writing Logic Code

Make good use of [WP’s own conditional tags](https://codex.wordpress.org/Conditional_Tags).
Use operators to combine conditions. See Supported functions and globals section
for allowed functions. Functions are extendable via filters.

 1.  **Operators**
 2.   * `!` (not) to **reverse** the logic, eg `!is_home()` is TRUE when this is NOT
        the home page.
      * `||` (or) to **combine** conditions. `X || Y` is TRUE when either X or Y is
        true.
      * `&&` (and) to make conditions **more specific**. `X && Y` is TRUE when both
        X and Y are true.
 3.  **WordPress Conditional Checks**
 4.   * `is_home()` — just the main blog page
      * `!is_page('about')` — everywhere except this specific WP page
      * `is_user_logged_in()` — shown when a user is logged in
      * `is_category(array(5,9,10,11))` — category page of one of the given category
        IDs
      * `is_single() && in_category('baked-goods')` — single post that’s in the “baked-
        goods” category
      * `current_user_can('administrator')` — admin-only blocks
      * `strpos($_SERVER['HTTP_REFERER'], "google.com") !== false` — blocks to show
        when clicked through from Google
      * `is_category() && in_array($cat, get_term_children(5, 'category'))` — category
        page that’s a descendant of category 5
      * `in_array(77, get_post_ancestors($post))` — WP page that is a child of page
        77
      * `is_page('home') || $post->post_parent == 13` — home page or the page that’s
        a child of page 13
 5.  **WordPress Post & Term Checks**
 6.   * `has_term('special-offer', 'category', $post)` — post has a specific category
        term
      * `get_post_meta(get_the_ID(), 'featured', true) == 'yes'` — check a meta field
        value
      * `get_post_type() == 'product'` — current post type is a WooCommerce product
      * `is_sticky()` — sticky posts only
      * `has_post_thumbnail()` — only posts/pages with a featured image
      * `get_the_category()` — fetch post categories (can be used in expressions)
      * `get_the_tags()` — fetch post tags
 7.  **WooCommerce-Specific Checks**
 8.   * `WC()->cart && WC()->cart->get_cart_contents_count() > 0` — cart has at least
        one item
      * `WC()->cart && WC()->cart->total > 50` — cart total is more than $50
      * `is_product() && get_post_meta(get_the_ID(), '_sale_price', true) !== ''` —
        product has a sale price
      * `is_product_category('shoes') && WC()->cart` — category page “shoes” and cart
        exists
      * `is_user_logged_in() && WC()->cart && WC()->cart->get_cart_contents_count()
        > 0` — logged-in user and cart has items
 9.  **ACF Checks (Read-only)**
 10.  * `get_field('featured') == true` — ACF field “featured” is true
      * `get_sub_field('discount') > 0` — ACF subfield “discount” has a value
      * `get_row() > 0` — there are ACF repeater rows
      * `get_field_object('product_info')['value'] !== ''` — check ACF field object
        value
 11. **PHP Helpers**
 12.  * `in_array('baked-goods', wp_get_post_categories($post))` — category array check
      * `strpos(get_permalink($post), 'sale') !== false` — check if permalink contains“
        sale”
      * `empty(get_post_meta(get_the_ID(), 'stock', true))` — post has no stock meta
      * `count(WC()->cart->get_cart()) > 1` — more than 1 item in cart
      * `time() > strtotime('2026-01-01')` — check current timestamp
      * `number_format(WC()->cart->total, 2)` — format cart total
      * `round(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.

 1.  **Allowed Superglobals**
 2.   * By default, only safe, read-only superglobals are available. Developers can
        extend this list via the `block_logic_allowed_superglobals` filter.
         - `$_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
            ```
        
 3.  **Allowed WordPress Globals**
 4.   * These global variables are accessible inside block logic expressions. Extend
        the list via the `block_logic_allowed_globals` filter.
         - `$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()
            ```
        
 5.  **Allowed WordPress Functions**
 6.   * Overview
         - `current_user_can('administrator')` — check user capabilities
         - `get_current_user_id()` — current user ID
         - `get_option('blogname')` — get site title
         - `get_post_meta(get_the_ID(), 'featured', true)` — read post meta
         - `get_post_type()` — post type slug
         - `get_the_ID()` — current post ID
         - `has_post_thumbnail()` — check if post has featured image
         - `is_page('about')` — check if current page is “about”
      * Example usage:
      *     ```
            is_page('about') && has_post_thumbnail()
            ```
        
 7.  **ACF Functions**
 8.   * Overview
         - `get_field('featured')` — read ACF field value
         - `get_sub_field('discount')` — read subfield in repeater
         - `get_row()` — number of repeater rows
         - `get_field_object('product_info')['value']` — field object value
      * Example usage:
      *     ```
            get_field('featured') == true && get_sub_field('discount') > 0
            ```
        
 9.  **WooCommerce Functions**
 10.  * Overview
         - `wc_get_cart_contents_count()` — number of items in cart
         - `wc_get_cart_total()` — cart total
         - `wc_get_cart_subtotal()` — cart subtotal
         - `wc_cart_is_empty()` — TRUE if cart is empty
         - `wc_customer_get_id()` — current customer ID
         - `wc_customer_get_billing_email()` — customer billing email
         - `wc_customer_get_shipping_country()` — shipping country
         - `wc_customer_get_shipping_state()` — shipping state
         - `wc_customer_get_shipping_postcode()` — shipping postcode
         - `wc_get_product($product_id)` — get WC_Product object
      * Example usage:
      *     ```
            wc_get_cart_contents_count() > 0 && wc_get_cart_total() > 50
            ```
        
 11. **PHP Helpers**
 12.  * 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')
            ```
        

### 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.

 1. **Add custom functions**
 2.  * Use the `block_logic_allowed_functions` filter 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()
           ```
       
 3. **Add extra superglobals**
 4.  * Use the `block_logic_allowed_superglobals` filter 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'])
           ```
       
 5. **Add extra WordPress globals**
 6.  * Use the `block_logic_allowed_globals` filter 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']
           ```
       
 7. **Add completely custom globals**
 8.  * Use the `block_logic_extra_globals` filter 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()
           ```
       

## 使用者評論

![](https://secure.gravatar.com/avatar/4c74cbdc2c55154ff89b236eabb79cc24844de7fa2ac7838121a41833375464b?
s=60&d=retro&r=g)

### 󠀁[Simple and Effective](https://wordpress.org/support/topic/simple-and-effective-1544/)󠁿

 [tommyae](https://profiles.wordpress.org/tommyae/) 2024 年 1 月 14 日 1 則留言

Beautiful plugin, solves many of my problems while being very light weight and efficient.
Many plugins try to solve too many things, probably because it motivates a price
tag. But when all you need is to solve a specific issue, plugins like this are a
god send.

![](https://secure.gravatar.com/avatar/1a7fa2ea77264c135f9882aa1fc47a05b47ad4efb3e5ef40edcc9931579fdd38?
s=60&d=retro&r=g)

### 󠀁[Great Plugin!](https://wordpress.org/support/topic/great-plugin-38016/)󠁿

 [jimbager](https://profiles.wordpress.org/jimbager/) 2023 年 11 月 26 日 1 則留言

It’s really powerful, flexible, and simple, though it needs some basic knowledge
of PHP.Thanks for sharing the plugin.

![](https://secure.gravatar.com/avatar/8bf4e001b356053691150698a558b4a9432963388308be0e0b0d7cfa51d78e01?
s=60&d=retro&r=g)

### 󠀁[Just works](https://wordpress.org/support/topic/just-works-763/)󠁿

 [rvnd](https://profiles.wordpress.org/rvnd/) 2023 年 11 月 9 日 1 則留言

none of the other Condition Block Visibility plugins had me up & successful in less
than a minute.

![](https://secure.gravatar.com/avatar/45e3dfb5e418c9d670a2c0b158440b929e2d64bf3a0f63c002d7ef1141e8e83a?
s=60&d=retro&r=g)

### 󠀁[Awesome!](https://wordpress.org/support/topic/awesome-10120/)󠁿

 [Nazar Hotsa](https://profiles.wordpress.org/bugnumber9/) 2023 年 11 月 6 日 1 
則留言

This plugin is absolutely AWESOME! Simple, fast and efficient. No bloat with tons
of pre-configured conditions for every popular plugin out there, no fancy UI that’s
often not needed, etc. Definitely a part of my toolset now.

![](https://secure.gravatar.com/avatar/84255361874182ae1ad5f652a9ef80825e8703eab82ecc89c0f9fdebf0473b70?
s=60&d=retro&r=g)

### 󠀁[Simple and works](https://wordpress.org/support/topic/simple-and-works-214/)󠁿

 [xokko](https://profiles.wordpress.org/xokko/) 2022 年 12 月 24 日 1 則留言

It’s easy to set up and works. I didn’t want more.

![](https://secure.gravatar.com/avatar/84a5e100cc4056514aa9a0cecd97389eba9e55c9e7c6f8e5c8b7f841446d43d1?
s=60&d=retro&r=g)

### 󠀁[Work With New WordPress Theme Editor](https://wordpress.org/support/topic/work-with-new-wordpress-theme-editor/)󠁿

 [johnzenausa](https://profiles.wordpress.org/johnzenausa/) 2022 年 8 月 17 日 1
則留言

Works just like widget logic. Excellent plugin and easy to use.

 [ 閱讀全部 13 則使用者評論 ](https://wordpress.org/support/plugin/block-logic/reviews/)

## 參與者及開發者

以下人員參與了開源軟體〈Block Logic – Full Block Display Control〉的開發相關工作。

參與者

 *   [ landwire ](https://profiles.wordpress.org/landwire/)

〈Block Logic – Full Block Display Control〉外掛目前已有 1 個本地化語言版本。 感謝
[全部譯者](https://translate.wordpress.org/projects/wp-plugins/block-logic/contributors)
為這個外掛做出的貢獻。

[將〈Block Logic – Full Block Display Control〉外掛本地化為台灣繁體中文版](https://translate.wordpress.org/projects/wp-plugins/block-logic)

### 對開發相關資訊感興趣？

任何人均可[瀏覽程式碼](https://plugins.trac.wordpress.org/browser/block-logic/)、
查看 [SVN 存放庫](https://plugins.svn.wordpress.org/block-logic/)，或透過 [RSS](https://plugins.trac.wordpress.org/log/block-logic/?limit=100&mode=stop_on_copy&format=rss)
訂閱[開發記錄](https://plugins.trac.wordpress.org/log/block-logic/)。

## 變更記錄

#### 2.1.6

Fixed a dependency and tested with 7.0.

#### 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

## 中繼資料

 *  版本 **2.1.6**
 *  最後更新 **2 個月前**
 *  啟用安裝數 **300+**
 *  WordPress 版本需求 ** 6.5 或更新版本 **
 *  已測試相容的 WordPress 版本 **7.0.3**
 *  PHP 版本需求 ** 8.0 或更新版本 **
 *  語言
 * [English (US)](https://wordpress.org/plugins/block-logic/) 及 [German](https://de.wordpress.org/plugins/block-logic/).
 *  [將這個外掛本地化為你的母語版本](https://translate.wordpress.org/projects/wp-plugins/block-logic)
 * 標籤:
 * [block](https://tw.wordpress.org/plugins/tags/block/)[conditional](https://tw.wordpress.org/plugins/tags/conditional/)
   [conditions](https://tw.wordpress.org/plugins/tags/conditions/)[logic](https://tw.wordpress.org/plugins/tags/logic/)
   [visibility](https://tw.wordpress.org/plugins/tags/visibility/)
 *  [進階檢視](https://tw.wordpress.org/plugins/block-logic/advanced/)

## 評分

 5 星，滿分為 5 星

 *  [  13 個 5 星使用者評論     ](https://wordpress.org/support/plugin/block-logic/reviews/?filter=5)
 *  [  0 個 4 星使用者評論     ](https://wordpress.org/support/plugin/block-logic/reviews/?filter=4)
 *  [  0 個 3 星使用者評論     ](https://wordpress.org/support/plugin/block-logic/reviews/?filter=3)
 *  [  0 個 2 星使用者評論     ](https://wordpress.org/support/plugin/block-logic/reviews/?filter=2)
 *  [  0 個 1 星使用者評論     ](https://wordpress.org/support/plugin/block-logic/reviews/?filter=1)

[Your review](https://wordpress.org/support/plugin/block-logic/reviews/#new-post)

[查看全部使用者評論](https://wordpress.org/support/plugin/block-logic/reviews/)

## 參與者

 *   [ landwire ](https://profiles.wordpress.org/landwire/)

## 技術支援

最近 2 個月解決的問題:

     總計 1 個問題，已解決 1 個

 [檢視技術支援論壇](https://wordpress.org/support/plugin/block-logic/)