前言
提交配置为“自定义(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;
} );
补充说明
更多细节可参考 官方文档。