OhhMuaOhhMua
  • Home
  • Hosting
    • Best Web Hosting
    • Free Web Hosting
    • VPS Hosting
  • WordPress
    • Solutions & Troubleshooting
    • Installation & Setup
    • Themes & Plugins
    • Security
  • Computer Tips
    • Gaming Errors & Solutions
    • PC Troubleshooting
    • Computer Hardware
  • Coupons & Deals
  • Contact Us
Reading: Most Effective Ways to Prevent SPAM in Contact Form 7
Share
Notification Show More
Font ResizerAa
OhhMuaOhhMua
Font ResizerAa
  • Computer Tips
  • Hosting
  • WordPress
Search
  • Home
  • WordPress
    • Installation & Setup
    • Security
    • Solutions & Troubleshooting
    • Themes & Plugins
    • Troubleshooting
  • Hosting
    • Free Web Hosting
    • VPS Hosting
    • Best Web Hosting
  • Computer Tips
    • Computer Hardware
  • Coupons & Deals
Have an existing account? Sign In
Follow US
Copyright © 2024 ohhmua. All rights reserved.
OhhMua > Blog > WordPress > Solutions & Troubleshooting > Most Effective Ways to Prevent SPAM in Contact Form 7
Solutions & Troubleshooting

Most Effective Ways to Prevent SPAM in Contact Form 7

ohhmua
Last updated: September 10, 2024 4:27 pm
ohhmua
Share
4 Min Read
Most Effective Ways to Prevent SPAM in Contact Form 7
Most Effective Ways to Prevent SPAM in Contact Form 7
SHARE
Contents
Preventing Spam by Checking If Users Have ScrolledConclusion:

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.

Most Effective Ways to Prevent SPAM in Contact Form 7
Most Effective Ways to Prevent SPAM in Contact Form 7

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!

You Might Also Like

My Google Discover traffic skyrocketed after I did these 10 things

How to Add a “Read More” and “Show Less” Button to Product Descriptions in WooCommerce

Blog, E-Commerce, or Forum? WordPress Covers Every Website Type!

How I Optimized Pagination and Skyrocketed Traffic

How to Pick the Ideal Blogging Platform for Your Needs

TAGGED:Block spam for Contact Form 7How to Stop Contact Form Spam in WordPressPrevent spam for Contact Form 7
Share This Article
Facebook Twitter Email Print
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Trending Stories

How to Fix Red Alert 2 Black Screen Issue on Windows 10,11
Gaming Errors & Solutions

How to Fix Red Alert 2 Black Screen Issue on Windows 10,11

September 18, 2024
wordpress.com vs wordpress.org
Themes & Plugins

WordPress.com vs WordPress.org: A Beginner’s Guide to Choosing the Right Platform

April 26, 2025
Fake CAPTCHA Malware Targets Gamers Seeking Cracked Wukong Downloads
Gaming Errors & Solutions

Fake CAPTCHA Malware Targets Gamers Seeking Cracked Wukong Downloads – How to Stay Safe

September 24, 2024
How I Fixed the No Audio Issue in Wuthering Waves (2025)
Gaming Errors & Solutions

How I Fixed the No Audio Issue in Wuthering Waves (2025)

May 11, 2025
Customizing Canonical Tags in WordPress
Solutions & Troubleshooting

Step-by-Step Tutorial: Customizing Canonical Tags in WordPress

September 15, 2024
Create a Simple Incrementing Number Effect with HTML and JavaScript 2
Solutions & Troubleshooting

Create a Simple Incrementing Number Effect with HTML and JavaScript

September 17, 2024

You Might Also Like

Protecting Your WordPress Site from File Upload Vulnerabilities
Solutions & Troubleshooting

Protecting Your WordPress Site from File Upload Vulnerabilities

September 26, 2024
Title and Meta Description Mismatch on Google
Solutions & Troubleshooting

Title and Meta Description Mismatch on Google[How to fix]

September 25, 2024
Contact button
Solutions & Troubleshooting

Contact button in the footer with added call button shake effect

September 21, 2024
Guide to Creating a Beautiful Promotion Notification Box for Your Website
Solutions & Troubleshooting

How to Create a Stunning Notification Box for Your Website

September 20, 2024
Previous Next
newsletter featured

Always Stay Up to Date

Subscribe to our newsletter to get our newest articles instantly!

Follow US on Social Media

Facebook Youtube Steam Twitch Unity

Copyright © 2024 ohhmua. All rights reserved.

OhhMua

Information

  • About
  • Terms & Conditions
  • Privacy Policy
Welcome Back!

Sign in to your account

Lost your password?