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
  • veo3 prompt generator
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
    • PC Troubleshooting
    • Gaming Errors & Solutions
    • Computer Hardware
  • Coupons & Deals
  • veo3 prompt generator
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

Cannot Fetch Sitemap in Google Search Console

Discovered – Currently Not Indexed in Google Search Console: What It Means & How to Fix It

Why 2 Backlinks per Article Might Be Killing Your SEO

What is Obsidian? Why Developers Love This Note-Taking App

Want Faster Load Times? Optimize WordPress This Way

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

How to Increase the Star Ratings for KK Star Ratings 1
Solutions & Troubleshooting

How to Increase the Star Ratings for KK Star Ratings

September 8, 2024
What is Obsidian
Solutions & Troubleshooting

What is Obsidian? Why Developers Love This Note-Taking App

May 20, 2025
How to Choose the Right Motherboard for Intel 14th Gen Processors
Computer Hardware

How to Choose the Right Motherboard for Intel 14th Gen Processors

April 22, 2025
Protecting Your WordPress Site from File Upload Vulnerabilities
Solutions & Troubleshooting

Protecting Your WordPress Site from File Upload Vulnerabilities

September 26, 2024
5 Signs Your SSD is About to Fail and How to Extend Its Lifespan
Computer Hardware

5 Signs Your SSD is About to Fail and How to Extend Its Lifespan

August 6, 2020
GPU Fan Not Spinning
Computer Hardware

GPU Fan Not Spinning: Causes and Solutions

July 22, 2023
Previous Next

You Might Also Like

My Google Discover traffic skyrocketed after I did these 10 things
Solutions & Troubleshooting

My Google Discover traffic skyrocketed after I did these 10 things

May 14, 2025
How to Add a Read More to Product Descriptions in WooCommerce
Solutions & Troubleshooting

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

May 5, 2025
WordPress Covers Every Website Type
Solutions & Troubleshooting

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

April 29, 2025
Optimized Pagination thumbnail
Solutions & Troubleshooting

How I Optimized Pagination and Skyrocketed Traffic

April 28, 2025
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?