To protect your WP Forms from spam, you can implement a simple spam protection technique by adding a hidden field to your form and checking if it remains empty when the form is submitted. This technique helps to prevent automated bots from submitting the form.
Here’s an example code snippet that demonstrates how to add a hidden field to your WP Forms:
/**
* Add a hidden field to WP Forms for spam protection.
*
* @param array $fields The array of form fields.
* @param int $form_id The ID of the form.
* @return array The modified array of form fields.
*/
function wpsnippets_add_spam_protection_field( $fields, $form_id ) {
// Add a hidden field with a unique name.
$fields['wpsnippets_spam_field'] = array(
'type' => 'hidden',
'value' => '',
);
return $fields;
}
add_filter( 'wpforms_form_fields', 'wpsnippets_add_spam_protection_field', 10, 2 );
In the code snippet above, we’re using the wpforms_form_fields filter to add a hidden field to the WP Forms. The wpsnippets_add_spam_protection_field function takes two parameters: $fields (the array of form fields) and $form_id (the ID of the form). Inside the function, we add a new field with the key 'wpsnippets_spam_field' and set its type to 'hidden'. The value of the hidden field is initially empty.
By adding this hidden field to your WP Forms, you can check if it remains empty when the form is submitted. If the field is not empty, it indicates that a bot has filled it out, and you can handle it accordingly.
Remember to customize the field name ('wpsnippets_spam_field') to something unique to avoid conflicts with other form fields.
This code snippet can be useful in any WP Forms where you want to add a simple spam protection technique.
Examples
Example 1: Implementing reCAPTCHA in WP Forms
This example demonstrates how to implement reCAPTCHA in WP Forms to protect against spam submissions.
function wpsnippets_add_recaptcha_to_wpforms( $form_id ) {
if ( function_exists( 'wpforms' ) && function_exists( 'wpsnippets_get_recaptcha_site_key' ) && function_exists( 'wpsnippets_get_recaptcha_secret_key' ) ) {
$site_key = wpsnippets_get_recaptcha_site_key();
$secret_key = wpsnippets_get_recaptcha_secret_key();
if ( ! empty( $site_key ) && ! empty( $secret_key ) ) {
wpforms()->form->builder->settings['recaptcha'] = [
'enabled' => true,
'site_key' => $site_key,
'secret_key' => $secret_key,
'version' => 'v2',
'size' => 'normal',
'badge' => 'bottomright',
'hide_badge' => false,
'error' => __( 'reCAPTCHA verification failed, please try again.', 'text-domain' ),
'error_class' => 'wpforms-error',
];
}
}
}
add_action( 'wpforms_builder_settings_default', 'wpsnippets_add_recaptcha_to_wpforms' );
This code adds reCAPTCHA to WP Forms by hooking into the wpforms_builder_settings_default action. It checks if the necessary functions and keys are available, and if so, sets the reCAPTCHA settings in the WP Forms builder settings array.
Example 2: Custom Spam Protection Question in WP Forms
This example demonstrates how to add a custom spam protection question to WP Forms to prevent spam submissions.
function wpsnippets_add_custom_spam_protection_question( $fields, $form_id ) {
if ( function_exists( 'wpforms' ) ) {
$fields[] = [
'type' => 'text',
'label' => 'What is 2 + 2?',
'name' => 'spam_protection',
'required' => true,
'validation' => 'equal=4',
];
}
return $fields;
}
add_filter( 'wpforms_builder_fields', 'wpsnippets_add_custom_spam_protection_question', 10, 2 );
This code adds a custom spam protection question to WP Forms by hooking into the wpforms_builder_fields filter. It appends a new field to the form fields array, which asks the user to input the answer to the question “What is 2 + 2?”. The field is set as required and validated to ensure the answer is equal to 4.
Example 3: Honeypot Spam Protection in WP Forms
This example demonstrates how to implement honeypot spam protection in WP Forms by adding a hidden field that should not be filled by users.
function wpsnippets_add_honeypot_spam_protection( $fields, $form_id ) {
if ( function_exists( 'wpforms' ) ) {
$fields[] = [
'type' => 'hidden',
'label' => 'Leave this field empty',
'name' => 'honeypot',
'required' => false,
];
}
return $fields;
}
add_filter( 'wpforms_builder_fields', 'wpsnippets_add_honeypot_spam_protection', 10, 2 );
This code adds honeypot spam protection to WP Forms by hooking into the wpforms_builder_fields filter. It appends a hidden field to the form fields array, which is labeled as “Leave this field empty”. The field is not required, but if it is filled by a spam bot, the form submission will be rejected.
