Bricks 中文文档:bricks/security_check_before_save/new_elements

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

📄 原始资料  |  展开查看原始 JSON(bricks-security_check_before_save-new_elements.json)
{
  "name": "bricks/security_check_before_save/new_elements",
  "kind": "filter",
  "url": "https://academy.bricksbuilder.io/developer/hooks/filters/bricks-security_check_before_save-new_elements/",
  "scraped_at": "2026-08-29",
  "hook_name": "bricks/security_check_before_save/new_elements",
  "description": {
    "en": "Filters the array of new elements before they are saved to the database, specifically during the security check process (e.g., when validating  tags). This allows you to inspect or modify the element data before it persists.",
    "zh": "在将新元素保存到数据库之前对其进行筛选,特别是在安全检查过程中(例如在验证  标签时)。允许你在元素数据持久化之前检查或修改元素数据。"
  },
  "parameters": [
    {
      "name": "new_elements",
      "type": "array",
      "type_zh": null,
      "description": {
        "en": "Array of new element data structures.",
        "zh": "新元素数据结构数组。"
      }
    },
    {
      "name": "old_elements_indexed",
      "type": "array",
      "type_zh": null,
      "description": {
        "en": "Array of existing elements, indexed by their ID, for comparison.",
        "zh": "已存在元素数组,按 ID 索引,用于比较。"
      }
    }
  ],
  "example_usage": "add_filter( 'bricks/security_check_before_save/new_elements', function( $new_elements, $old_elements_indexed ) {\n    // Example: Loop through new elements and log changes\n    foreach ( $new_elements as $element ) {\n        // Custom logic here\n    }\n\n\n    return $new_elements;\n}, 10, 2 );"
}

前言

在将新元素保存到数据库之前对其进行筛选,特别是在安全检查过程中(例如在验证 标签时)。允许你在元素数据持久化之前检查或修改元素数据。

用法

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

add_filter( 'bricks/security_check_before_save/new_elements', function( $new_elements, $old_elements_indexed ) {
    // Example: Loop through new elements and log changes
    foreach ( $new_elements as $element ) {
        // Custom logic here
    }


    return $new_elements;
}, 10, 2 );

参数

  • $new_elements (array):新元素数据结构数组。
  • $old_elements_indexed (array):已存在元素数组,按 ID 索引,用于比较。

示例代码

筛选返回值

add_filter( 'bricks/security_check_before_save/new_elements', function( $new_elements, $old_elements_indexed ) {
    // Example: Loop through new elements and log changes
    foreach ( $new_elements as $element ) {
        // Custom logic here
    }


    return $new_elements;
}, 10, 2 );

根据条件短路返回原值

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

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

补充说明

更多细节可参考 官方文档