Bricks 中文文档:bricks/maintenance/should_apply

📄 原始资料  |  展开查看原始 JSON(bricks-maintenance-should_apply.json){ “name”: “bricks/maintenance/should_apply”, “kind”: “filter”, “url”: “https://academy.bricksbuilder.io/developer/hooks/filters/filter-bricks-maintenance-should_apply/”, “scraped_at”: “2026-08-29”, “hook_name”: “bricks/maintenance…

📄 原始资料  |  展开查看原始 JSON(bricks-maintenance-should_apply.json)
{
  "name": "bricks/maintenance/should_apply",
  "kind": "filter",
  "url": "https://academy.bricksbuilder.io/developer/hooks/filters/filter-bricks-maintenance-should_apply/",
  "scraped_at": "2026-08-29",
  "hook_name": "bricks/maintenance/should_apply",
  "description": {
    "en": "Use this filter to override whether maintenance mode should be enforced for the current request. By default, Bricks checks if the user is in the admin area or builder, if the current page is the login page, if the user can bypass maintenance, or if the current post is in the excluded list. This filter runs after all of those checks have passed, giving you a final opportunity to apply custom logic before showing the maintenance or coming soon page. In the following example, anyone with the correct preview_key in the URL (e.g. https://domain.com?preview_key=letmein123 ) will bypass maintenance mode. This is especially useful for sharing a live preview with clients or collaborators without requiring login access. You can improve this behavior further by persisting the preview access across page loads using a cookie. Otherwise, the user would only bypass maintenance on the first page they visit (where the key is present in the URL), and get blocked again when navigating elsewhere.",
    "zh": "筛选当前页面的 Bricks 内容数据。"
  },
  "since": "2.0",
  "parameters": [
    {
      "name": "apply_maintenance",
      "type": "bool",
      "type_zh": null,
      "description": {
        "en": "Whether maintenance mode should apply. Defaults to true after all core checks pass.",
        "zh": "是否应用维护模式。在所有核心检查通过后默认为 true。"
      }
    },
    {
      "name": "mode",
      "type": "string",
      "type_zh": null,
      "description": {
        "en": "The current maintenance mode. Either 'maintenance' or 'coming_soon' .",
        "zh": "当前的维护模式。可以是 'maintenance' 或 'coming_soon'。"
      }
    }
  ],
  "example_usage": "add_filter( 'bricks/maintenance/should_apply', function( $apply_maintenance, $mode ) {\n    $valid_key = 'letmein123';\n\n\n    // Bypass maintenance mode if a valid preview key is present in the URL\n    if ( isset( $_GET['preview_key'] ) && $_GET['preview_key'] === $valid_key ) {\n        return false;\n    }\n\n\n    // Keep default behavior for all other cases\n    return $apply_maintenance;\n}, 10, 2 );"
}

前言

筛选当前页面的 Bricks 内容数据。

用法

将代码加入子主题的 functions.php

add_filter( 'bricks/maintenance/should_apply', function( $apply_maintenance, $mode ) {
    $valid_key = 'letmein123';


    // Bypass maintenance mode if a valid preview key is present in the URL
    if ( isset( $_GET['preview_key'] ) && $_GET['preview_key'] === $valid_key ) {
        return false;
    }


    // Keep default behavior for all other cases
    return $apply_maintenance;
}, 10, 2 );

参数

  • $apply_maintenance (bool):是否应用维护模式。在所有核心检查通过后默认为 true。
  • $mode (string):当前的维护模式。可以是 ‘maintenance’ 或 ‘coming_soon’。

示例代码

筛选返回值

add_filter( 'bricks/maintenance/should_apply', function( $apply_maintenance, $mode ) {
    $valid_key = 'letmein123';


    // Bypass maintenance mode if a valid preview key is present in the URL
    if ( isset( $_GET['preview_key'] ) && $_GET['preview_key'] === $valid_key ) {
        return false;
    }


    // Keep default behavior for all other cases
    return $apply_maintenance;
}, 10, 2 );

根据条件短路返回原值

根据条件跳过或修改返回值:

add_filter( 'bricks/maintenance/should_apply', function( $value, $apply_maintenance ) {
    if ( is_admin() ) { return $value; }
    // TODO: custom logic here
    return $value;
} );

补充说明

更多细节可参考 官方文档