Are you troubled by receiving a large number of spam messages through your contact form?
Specifically, spam messages with the following content are frequently being sent:
- Sales emails, investment solicitations, dating, adult content
- Strings of English text or nonsensical content
- Promotional messages containing suspicious links
- Submissions sent repeatedly in a short time from the same IP address
As a result, there is a risk of missing legitimate inquiries from actual users, and it increases the operational burden.
As countermeasures, we are considering the following approaches:
- Enhancing the NG word filter
- Blocking specific IP addresses or domains
- Implementing reCAPTCHA
- Spam bot measures such as honeypot functionality
To set NG words (prohibited terms) in Contact Form 7 (CF7), you can implement it using one of the following methods:
✅ Method 1: Add a filter hook to functions.php (Recommended)
Use CF7’s wpcf7_validate_*
hook to detect NG words and return a validation error.
Example: Setting NG words for a text field (your-message
)
add_filter('wpcf7_validate_textarea', 'custom_ng_word_validation', 10, 2); function custom_ng_word_validation($result, $tag) { $name = $tag->name; // Check target field name if ($name === 'your-message') { $value = isset($_POST[$name]) ? $_POST[$name] : ''; // List of NG words (add as needed) $ng_words = ['Earn money', '100% free', 'Meet singles']; foreach ($ng_words as $ng) { if (stripos($value, $ng) !== false) { $result->invalidate($tag, 'Submission rejected due to suspected spam content.'); break; } } } return $result; }
✅ Method 2: Example for multiple fields
add_filter('wpcf7_validate_text', 'custom_ng_word_validation_all_fields', 10, 2); add_filter('wpcf7_validate_textarea', 'custom_ng_word_validation_all_fields', 10, 2); function custom_ng_word_validation_all_fields($result, $tag) { $name = $tag->name; $value = isset($_POST[$name]) ? $_POST[$name] : ''; // List of NG words $ng_words = ['Earn money', '100% free', 'Meet singles']; foreach ($ng_words as $ng) { if (stripos($value, $ng) !== false) { $result->invalidate($tag, 'Submission rejected due to suspected spam content.'); break; } } return $result; }
🔍 Notes
- Since
stripos()
is used, it is case-insensitive. - Customize the warning message returned to the sender to match the tone of your site.
- The
*
inwpcf7_validate_*
corresponds to the field type (text
,textarea
,email
, etc.).
🚀 Advanced Use Cases
- If NG words should only be rejected on an exact match → use
===
orin_array()
- Manage NG words via the database or options page → retrieve them using
get_option()
, etc.