Say i have a form with the following fields, “Name”, “Email” and “Message”.
When the form is submitted, i want to send the data to a dedicated table within my WP database having similar fields as the forms.
The code in the URL below achieves this for Elementor Forms. Can this be modified for Bricks? Please Help!
I use this code from the Bricks academy. Add the code into a WPCodeBox snippet or into the functions.php file.
I added the standard WP function “wpdb->insert” to the code, so Wordpress takes care of everything including preparing the data and sending the data as a new row to the database. Gotta love Wordpress, right?
How to do it:
Replace the table name “wp_test” with your WP table name in the code.
Replace the id number (the xxxxxx) in [‘form-field-xxxxxx’] in the code with the 6 digit id number from your Bricks form fields in the code.
function my_form_custom_action( $form ) {
$fields = $form->get_fields();
// $formId = $fields['formId'];
// $postId = $fields['postId'];
$settings = $form->get_settings();
// $files = $form->get_uploaded_files();
// Perform some logic here...
global $wpdb;
$wpdb->insert("wp_test", array(
"Name" => $fields['form-field-256ffd'],
"Email" => $fields['form-field-d64e13'],
"Message" => $fields['form-field-ea931b'],
));
// Set result in case it fails
$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 );
As far as I understand, this custom action is called when any form is submitted that has the custom action trigger. So if you have more than 1 form with the custom action trigger, you would have to also make a check that the database is only updated if the specific form for that case is submitted.
I know you’ve got your answer, but I’m just putting this here for those who just want an easy way to save all bricks forms into the database with no fuss.
Once installed, it just works with no further configuration.
Note: I’m not the owner of it or affiliated with this plugin in any way.