One of the most frustrating issues when adding a form to your website is dealing with relentless spam from bots. If you’ve been receiving spam emails from Contact Form 7 (CF7) constantly, despite trying various solutions, this article will guide you on how to effectively block and stop those automated spam submissions once and for all.
Recently, someone reached out to me, struggling with CF7 spam even after using Akismet, reCAPTCHA, Wordfence .Luckily, I had a custom-made code that I shared with them, and it worked wonders! They reported that the spam stopped entirely, so I’m excited to share this solution with you as well. Give it a try, and say goodbye to CF7 spam!
Preventing Spam by Checking If Users Have Scrolled
I believe this is one of the simplest and most effective ways to prevent spam. It’s easy for users and limits auto-spam submissions.
Mechanism: Forms are usually placed in the middle or at the bottom of the website, so users have to scroll down to reach the form and enter their information before submitting, right? Therefore, we can track how much the user has scrolled, and if it reaches a certain point that seems appropriate, we allow the form to be submitted.
Advantages:
- It doesn’t annoy users.
- It’s simple and doesn’t affect the form’s interface.
Disadvantages:
- At the moment, I haven’t found any downsides! 😀
Implementation: Simply add the following code to wp-content/themes/{your-theme}/functions.php
.
/*
* Check spam for CF7 by scroll
* Author: ohhmua.com
*/
add_filter('wpcf7_form_elements', 'devvn_check_scroll_form_cf7');
function devvn_check_scroll_form_cf7($html){
$html = '<div style="display: none"><p><span class="wpcf7-form-control-wrap" data-name="devvn-scroll"><input size="40" class="wpcf7-form-control wpcf7-text" aria-invalid="false" value="0" type="text" name="devvn-scroll"></span></p></div>' . $html;
return $html;
}
add_action('wpcf7_posted_data', 'devvn_check_scroll_form_cf7_vaild');
function devvn_check_scroll_form_cf7_vaild($posted_data) {
$submission = WPCF7_Submission::get_instance();
$scroll = isset($posted_data['devvn-scroll']) ? intval($posted_data['devvn-scroll']) : 0;
// If the form is placed near the top of the page, replace 5000 with a smaller number, e.g., 200
if (!$scroll || $scroll <= 5000) {
$submission->set_status( 'spam' );
$submission->set_response( 'You are a spammer' );
}
unset($posted_data['devvn-scroll']);
return $posted_data;
}
add_action('wp_footer', function (){
?>
<script>
const scrollInputs = document.querySelectorAll('input[name="devvn-scroll"]');
if(scrollInputs.length > 0) {
let accumulatedScroll = 0;
function devvnCheckScroll() {
accumulatedScroll += window.scrollY;
scrollInputs.forEach(input => {
input.value = accumulatedScroll;
});
// If the form is placed near the top of the page, replace 6000 with a smaller number, e.g., 300
if (accumulatedScroll >= 6000) {
window.removeEventListener('scroll', devvnCheckScroll);
}
}
window.addEventListener('scroll', devvnCheckScroll);
}
</script>
<?php
});
In the code, there are two values, 5000 and 6000, as I’ve noted directly in the code. These numbers depend on the position of the form on your website, but I believe they are fine as-is. You can adjust them as needed.
Conclusion:
That’s it! These are the methods I wanted to share with you to prevent spam. I hope this helps you block those annoying spam bots!