The article discusses how to prevent spam for the Contact Form 7 plugin by using the Akismet plugin, which automatically filters out comment spam. Akismet is developed by Automattic and uses algorithms to detect and block spam comments. To use Akismet, a free API key can be obtained for personal blogs, while commercial websites require a paid subscription. The article provides steps to integrate Akismet with Contact Form 7 to filter out spam in name, email, URL, and phone number fields. Additionally, it includes code for validating phone numbers in the Contact Form 7 plugin for Vietnamese numbers.
Implementing Spam Protection for Contact Form 7 Using Akismet Plugin
Akismet is an automated spam filtering tool. The name Akismet is a combination of “Automattic” and “Kismet.” Automattic is the company behind the Akismet plugin, founded by Matt Mullenweg, the co-creator of WordPress. Akismet detects spam comments and pingbacks through its algorithm. This algorithm learns from its mistakes and actions taken by the community.
For instance, when certain websites start flagging a specific type of comment content as spam, Akismet learns to identify similar content as spam in the future.
Step 1: Install Akismet Anti-Spam Plugin
Akismet is the best way in the world to protect your blog from spam. Your site will be fully configured and protected.
You need an API key to use Akismet. For personal blogs, you can get a free API key. For company or commercial websites, paid subscriptions are available.
Step 2: Using Akismet with Contact Form 7
In Contact Form 7, there are 3 fields: your-name, your-email, and your-url that can be filtered using Akismet. Here’s what you can do:
In your form, where you want to filter spam, edit one of the fields as shown below and save the changes.
Filter spam for the field where the sender enters their Name:
[text* your-name akismet:author]
Filter spam for the field where the sender enters their Email:
[email* your-email akismet:author_email]
Filter spam for the field where the sender enters their Url:
[url* your-url akismet:author_url]
To implement Akismet in Contact Form 7, use one or more of these options. For better accuracy, use more options.
Step 3: Testing the Spam Filter
To check if the spam filter is working correctly, try entering “viagra-test-123” in the name field (akismet:author) or “akismet-guaranteed-spam@example.com” in the email field (akismet:author_email) and submit the form. With these test words, Akismet should respond with “spam.” If it’s working as expected, you will see a message saying, “There was an error trying to send your message. Please try again later,” surrounded by an orange border.
Validating Phone Number Spam Protection in Contact Form 7
Another field prone to spam in Contact Form 7 is the phone number field. Now, let me guide you on how to validate the phone number field in Contact Form 7 to match Vietnamese phone numbers, starting with “0” and having 10 digits.
- Customer must enter exactly 10 digits:
The code is:
[tel* your-phone minlength:10 maxlength:10]
Where:
minlength:10 is the minimum allowed length for this input field. maxlength:10 is the maximum allowed length for this input field.
- Customers must enter a valid area code starting with “0” for Vietnamese network providers:
To validate phone numbers in Contact Form 7, we need to add the following code to the functions.php file of your theme or child theme.
Check that all area codes of current Vietnamese network providers are correctly entered before allowing submission.
//validate số điện thoại trong Contact Form 7
function validate_phone_wpcf7( $result, $tel ){
$result = preg_match( ‘/^(09|03|07|08|05)+([0-9]{8})$/’, $tel );
return $result;
}
add_filter( ‘wpcf7_is_tel’, ‘validate_phone_wpcf7’, 10, 2 );
Dưới đây là code phiên bản đầy đủ:
function validate_phone_wpcf7( $result, $tel ) {
$result = preg_match( ‘/^(032|033|034|035|036|037|038|039|086|096|097|098|081|082|083|084|085|088|091|094|056|058|092|070|076|077|078|079|089|090|093|099|059)+([0-9]{7})$/;’, $tel );
return $result;
}
add_filter( ‘wpcf7_is_tel’, ‘validate_phone_wpcf7’, 10, 2 );
Chúc bạn thành công!