AI Verified

Name

Contact Form 7 Spam Protection

About

Contact Form 7 Spam Protection: Protect your contact forms from spam

Language

PHP

Rating

Voted: 0 by 0 user(s)

How to Setup Snippet

This code adds a simple math problem to your comment form that a user must solve before submitting a comment. It’s a basic form of CAPTCHA that can help deter spam bots. Please note that this is a basic solution and may not be sufficient for larger sites or sites with more aggressive spam. For those, a dedicated spam protection plugin or service would be recommended. Also, remember to replace 'comment_form_after_fields' and 'preprocess_comment' with the appropriate hooks for your Contact Form 7 form. Disclaimer: Always backup your site and test any changes in a staging environment before modifying your live site. This code is provided as is and you should ensure it fits your needs and is implemented correctly.

Codevault

Mairaj-Uddin-Ahmed

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.4

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

12/01/2024

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Found 0 vulnerabilities

Contact Form 7 Spam Protection

 
                    
1/**
2 * Display custom captcha in the comment form.
3 */
4function my_custom_captcha() {
5 $num1 = rand(1, 9);
6 $num2 = rand(1, 9);
7 
8 echo '<p><label>What is ' . $num1 . ' + ' . $num2 . '? (Anti-spam)</label><br />';
9 echo '<input type="text" name="my_captcha" /></p>';
10 echo '<input type="hidden" name="num1" value="' . $num1 . '" />';
11 echo '<input type="hidden" name="num2" value="' . $num2 . '" />';
12}
13add_action('comment_form_after_fields', 'my_custom_captcha');
14 
15/**
16 * Check custom captcha before processing the comment.
17 *
18 * @param array $commentdata Comment data.
19 * @return array Comment data.
20 */
21function check_my_captcha($commentdata) {
22 $num1 = isset($_POST['num1']) ? $_POST['num1'] : '';
23 $num2 = isset($_POST['num2']) ? $_POST['num2'] : '';
24 $my_captcha = isset($_POST['my_captcha']) ? $_POST['my_captcha'] : '';
25 
26 if ($num1 + $num2 != $my_captcha) {
27 wp_die('You failed the anti-spam check.');
28 }
29 
30 return $commentdata;
31}
32add_filter('preprocess_comment', 'check_my_captcha');

0

Related Snippets

Please see some snippets below related to this snippet..

General

AI Verified

0

Duplicate Post/Page (Including ACF Fields)

Added: 8 months ago

Last Updated: 7 months ago

<p>This WordPress code snippet enables a "Duplicate" action link in the admin panel for each post and page. Clicking on this link will create a draft copy of the post/page, including all Advanced Cust...

General

Unverified

0

CSV File validator with user-defined class and its properties

Added: 6 months ago

Last Updated: 6 months ago

General

AI Verified

0

Yabe Webfont - Proxy Google Fonts CDN

Added: 8 months ago

Last Updated: 7 months ago

Some countries block Google Fonts CDN. If you are in one of those countries, you can use the hosted Wakufont server to proxy the Google Fonts file URL. This feature is available on all pricing plans....

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

General

AI Verified

0

WhatsApp and Call Icon

Added: 5 months ago

Last Updated: 5 months ago

Display Sticky WhatsApp and Call image/icon on website

General

AI Verified

0

Contact Form 7 Spam Protection

Added: 5 months ago

Last Updated: 5 months ago

Contact Form 7 Spam Protection: Protect your contact forms from spam

WordPress Admin

AI Verified

0

Replace the 'Howdy' message on the Admin Bar.

Added: 5 months ago

Last Updated: 5 months ago

Replace the 'Howdy' message on the Admin Bar.