Bricks is a popular form builder for WordPress that allows you to create custom forms with ease. But sometimes, you may need to perform specific actions when a form is submitted, such as inserting data into a custom table or sending an email. This is where custom actions come in handy. In this blog post, we’ll show you how to add a custom action to a Bricks form.
Step 1: Understanding the Code
The following code adds a custom action “my_custom_action” to the Bricks form. The action is performed when the form is submitted. The code retrieves the form fields and settings and performs some logic (currently inserting data into a custom table in a WordPress database). If the logic fails, the form result is set with a custom message and type (success, danger, info).
function my_form_custom_action( $form ) { $fields = $form->get_fields(); $settings = $form->get_settings(); global $wpdb; $wpdb->insert("wpq1_my_custom_table", array( "Oruko" => $fields['form-field-152603'], "maili" => $fields['form-field-495c45'], "mess" => $fields['form-field-45d299'], )); $form->set_result([ 'action' => 'my_custom_action', 'type' => 'success', //or danger or info 'message' => esc_html__('Oh my custom action failed', 'bricks'), ]); } add_action( 'bricks/form/custom_action', 'my_form_custom_action', 10, 1 );
Step 2: Adding the Code to Your WordPress Site
To add this code to your WordPress site, you can add it to the theme’s functions.php file or create a custom plugin.
Step 3: Customizing the Code
In this code, the custom action is currently set to insert data into a custom table in the WordPress database. You can customize the code to perform any action you need when the form is submitted. For example, you can send an email or save the form data to a custom post type.
Step 4: Testing the Custom Action
Once you’ve added the code to your WordPress site, test the custom action by submitting a form on your website. If everything is set up correctly, the custom action should be performed when the form is submitted.