Spam bots are automated programs designed to submit forms indiscriminately, often with malicious intent. By integrating honeypot validation into your Bricks Builder forms, you can create a hidden trap for these bots, effectively filtering out illegitimate submissions while maintaining a seamless user experience.
Honeypot Validation Filter
To implement honeypot validation in Bricks Builder forms, we’ll utilize a filter that targets specific form IDs and checks for the presence of hidden honeypot fields. Here’s how the filter works:
add_filter( 'bricks/form/validate', function( $errors, $form ) {
$form_settings = $form->get_settings();
$form_fields = $form->get_fields();
$form_id = $form_fields['formId'];
// Skip validation: Form ID is not 'fdehur'
if ( $form_id !== 'fdehur' ) {
// Early return the $errors array if it's not the target form
return $errors;
}
// Get submitted field values of honeypot fields
$honeypot_first_name = $form->get_field_value( 'gdbgxf' );
$honeypot_last_name = $form->get_field_value( 'eswymk' );
// Error: Honeypot fields are not empty (indicating bot submission)
if ( $honeypot_first_name || $honeypot_last_name ) {
// Add error message to the $errors array
$errors[] = esc_html__( 'Sorry, you are a bot.', 'bricks' );
}
// Make sure to always return the $errors array
return $errors;
}, 10, 2 );
Disabling Autocomplete
Additionally, to enhance security and prevent autocomplete on form fields, we can include a JavaScript snippet:
<script>
let tagArr = document.getElementsByTagName("input");
for (let i = 0; i < tagArr.length; i++) {
tagArr[i].autocomplete = 'none';
}
</script>By implementing honeypot validation and disabling autocomplete in Bricks Builder forms, you can effectively combat spam bots and protect your website from unwanted submissions. These simple yet powerful techniques ensure that your forms remain secure and your inbox stays free from spam.
