Using Hooks in Form Builder
Form Builder provides hooks that allow you to customize the behavior and appearance of your forms. These hooks enable you to inject HTML code, validate form data, and perform actions before saving or sending the form. Below are the available hooks and their purposes:
form_builder_header
The form_builder_header
hook runs on every form page load and allows you to add HTML code to be displayed in the form page header. This can be useful for adding custom stylesheets, JavaScript libraries, or any other elements you want to include at the top of the form page.
<?php add_hook('form_builder_header', 1, function($vars = []) { $return = ''; if ($vars['id'] == 20) { $return = '<b>This is a custom output on the header in test form</b>'; } return $return; });
form_builder_footer
The form_builder_footer
hook also runs on every form page load and allows you to add HTML code to be displayed in the form page footer. This hook is useful for including additional scripts, tracking codes, or any other content you want to appear at the bottom of the form page.
<?php add_hook('form_builder_footer', 1, function($vars = []) { $return = ''; if ($vars['id'] == 20) { $return = '<b>This is a custom output on the footer in test form</b>'; } return $return; });
form_builder_validate
The form_builder_validate
hook runs when end clients submit form details to the server. This hook allows you to perform custom validation on the form data and apply any additional logic before processing the form submission.
Hook Parameters:
- id : Form ID
- title: Form Title
- fields: forms fields via data
<?php
add_hook('form_builder_validate
', 1, function($vars = []) {
$errors = [];
if ($vars['id'] == 20 && $vars['params'][0]['cid'] == 'f12345' && $vars['params'][0]['value'] == '17') {
$errors[] = 'We cannot accept your form due to your age.';
}
return $errors;
});
form_builder_pre_save
The form_builder_pre_save
hook executes after successful validation and just before saving or sending the form. This hook allows you to perform additional actions or modifications on the form data before it is saved or sent, such as integrating with external services, manipulating the data format, or performing any necessary preprocessing steps.
Hook Parameters:
- id : Form ID
- title: Form Title
- fields: forms fields via data
<?php add_hook('form_builder_pre_save
', 1, function($vars = []) { $new_values = []; if ($vars['id'] == 20 && $vars['params'][0]['cid'] == 'f12345' && $vars['params'][0]['value'] == '17') { $new_values[$vars['params'][0]['cid']] = 'New Value'; } if ($vars['id'] == 20){ $new_values['abort_save
'] = true; } return $new_values; });
To abort the saving form, return the key/value pair abort_save=true
To abort send client email, return the key/value pair client_email=true
To abort send admin email, return the key/value pair admin_email=true