Bricks 中文文档:bricks/form/custom_action

标签TAGS: 

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

📄 原始资料  |  展开查看原始 JSON(bricks-form-custom_action.json)
{
  "name": "bricks/form/custom_action",
  "kind": "action",
  "url": "https://academy.bricksbuilder.io/developer/hooks/actions/bricks-form-custom_action/",
  "scraped_at": "2026-08-29",
  "hook_name": "bricks/form/custom_action",
  "description": {
    "en": "Runs when a form with a “Custom” action is submitted. This allows you to execute custom PHP logic to handle the form submission.",
    "zh": "提交配置为“自定义(Custom)”动作的表单时触发。允许你执行自定义 PHP 逻辑来处理表单提交。"
  },
  "parameters": [
    {
      "name": "form",
      "type": "Bricks\\Form",
      "type_zh": null,
      "description": {
        "en": "The form instance.",
        "zh": "表单实例。"
      }
    }
  ],
  "example_usage": "add_action( 'bricks/form/custom_action', function( $form ) {\n    // Get form fields\n    $fields = $form->get_fields();\n\n\n    // Get form settings\n    $settings = $form->get_settings();\n\n\n    // Get submitted data\n    $form_data = $fields['formId'] ?? []; // Or retrieve specific field values\n\n\n    // Perform custom logic, e.g., send to external API\n    $name = isset( $fields['form-field-name'] ) ? $fields['form-field-name'] : '';\n\n\n    if ( $name === 'Specific Name' ) {\n        // Do something\n        $form->set_result( [\n            'type' => 'success', // or 'danger', 'info', 'warning'\n            'message' => esc_html__( 'Custom action executed successfully.', 'bricks' ),\n        ] );\n    }\n} );"
}

前言

提交配置为“自定义(Custom)”动作的表单时触发。允许你执行自定义 PHP 逻辑来处理表单提交。

用法

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

add_action( 'bricks/form/custom_action', function( $form ) {
    // Get form fields
    $fields = $form->get_fields();


    // Get form settings
    $settings = $form->get_settings();


    // Get submitted data
    $form_data = $fields['formId'] ?? []; // Or retrieve specific field values


    // Perform custom logic, e.g., send to external API
    $name = isset( $fields['form-field-name'] ) ? $fields['form-field-name'] : '';


    if ( $name === 'Specific Name' ) {
        // Do something
        $form->set_result( [
            'type' => 'success', // or 'danger', 'info', 'warning'
            'message' => esc_html__( 'Custom action executed successfully.', 'bricks' ),
        ] );
    }
} );

参数

  • $form (Bricks\Form):表单实例。

示例代码

官方示例用法

add_action( 'bricks/form/custom_action', function( $form ) {
    // Get form fields
    $fields = $form->get_fields();


    // Get form settings
    $settings = $form->get_settings();


    // Get submitted data
    $form_data = $fields['formId'] ?? []; // Or retrieve specific field values


    // Perform custom logic, e.g., send to external API
    $name = isset( $fields['form-field-name'] ) ? $fields['form-field-name'] : '';


    if ( $name === 'Specific Name' ) {
        // Do something
        $form->set_result( [
            'type' => 'success', // or 'danger', 'info', 'warning'
            'message' => esc_html__( 'Custom action executed successfully.', 'bricks' ),
        ] );
    }
} );

根据条件短路返回原值

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

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

补充说明

更多细节可参考 官方文档