WordPress comment spam is a major headache for website owners. If you’re tired of dealing with endless spammy comments filled with links, casino promotions, and scammy offers, you’re not alone!
Most people rely on Akismet or other spam-blocking plugins, but what if I told you that a tiny piece of code could do the same job without slowing down your site?
In this post, I’ll share a simple WordPress function that will automatically block spam comments based on links and common spam words, without needing any plugins.
Why You Should Avoid Spam Comments
Spam comments are more than just annoying; they can also:
- Harm your SEO – Search engines may penalize your site for spammy content.
- Damage credibility – Visitors lose trust if your site is full of scammy comments.
- Increase moderation workload – Manually removing spam takes time.
Simple Code to Instantly Block Spam Comments in WordPress
function auto_mark_spam_if_contains_links_or_spam_words($approved, $commentdata) {
// List of common spam words
$spam_words = array(
'free', 'win', 'winner', 'prize', 'guaranteed', 'congratulations', 'click here', 'earn money', 'extra cash',
'work from home', 'cheap', 'discount', 'buy now', 'limited offer', 'order now', 'risk free', 'exclusive deal',
'miracle', 'weight loss', 'fast money', 'no cost', 'increase sales', 'make money online', 'money back',
'investment', 'lottery', 'casino', 'betting', 'gamble', 'poker', 'roulette', 'blackjack', 'viagra',
'cialis', 'pharmacy', 'prescription', 'loan', 'debt', 'credit card', 'insurance', 'financial freedom',
'bitcoin', 'cryptocurrency', 'forex', 'trading', 'real estate', 'cheap flights', 'hotel deals',
'escort', 'dating', 'singles', 'adult', 'nude', 'porn', 'xxx', 'sex', 'cams', 'live streaming',
'watch free', 'pay per click', 'traffic boost', 'seo service', 'backlinks', 'web traffic',
'email marketing', 'bulk email', 'sms marketing', 'hurry', 'act now', 'apply now', 'special promotion',
'hot deal', 'gift card', 'survey', 'bonus', 'jackpot', 'millionaire', 'rich', 'earn fast',
'money making', 'payday', 'instant access', 'free access', 'subscribe now', 'double your income',
'easy income', 'secret trick', 'hidden secret', 'as seen on', 'get rich quick', 'quick cash',
'online income', 'be your own boss', 'investment plan', 'refinance', 'discount coupon',
'cheap rates', 'special discount', '100% free', 'hot girls', 'naked', 'live chat', 'hookup',
'dating site', 'make quick money', 'instant money', 'download now', 'financial help'
);
// Check if comment contains a link
if (preg_match('/https?:\/\/[^\s]+/', $commentdata['comment_content'])) {
return 'spam';
}
// Check for spammy words in comment content
foreach ($spam_words as $word) {
if (stripos($commentdata['comment_content'], $word) !== false) {
return 'spam'; // Mark comment as spam if any spammy word is found
}
}
return $approved; // Return normal approval status if no spam detected
}
add_filter('pre_comment_approved', 'auto_mark_spam_if_contains_links_or_spam_words', 99, 2);
How This WordPress Code to Block Spam Comments Works?
- Checks for links – If a comment contains
http://
orhttps://
, it’s automatically marked as spam. - Checks for spammy words – The function scans the comment for 100 common spam words (like “earn money,” “casino,” “discount,” etc.).
- No extra plugins needed – Keeps your WordPress site lightweight and fast.
Benefits of Using This Code Instead of a Plugin
Feature | This Code | Akismet (or Other Plugins) |
---|---|---|
Blocks comments with links | ✅ Yes | ✅ Yes |
Pricing | ✅ 100% Free, Forever, For Unlimited Comments | ❌ No, Free for Limited No. of Comments |
Customizable | ✅ 100% customizable for individual requirements | ❌ Not customizable at all |
Detects spam words | ✅ Yes | ✅ Yes |
Free to use | ✅ Yes | ❌ No (Paid for commercial use) |
Keeps site fast | ✅ Yes (No bloat) – Runs only whenever a comment is posted. | ❌ No (Extra database queries) |
Works without API keys | ✅ Yes | ❌ No (Akismet requires an API key) |
How to Add This Spam Comments Prevention Code to Your Site?
You can add this code to your wordpress site is a very simple way:
- Open your WordPress Dashboard.
- Go to Appearance → Theme File Editor.
- Open
functions.php
and paste the above code. - Click Update File and you are done, 99% of the spam comments will automatically go to the spam folder.
With this tiny but powerful code, you can block up to 99% of WordPress spam comments without relying on plugins like Akismet. It’s lightweight, effective, and keeps your website spam-free while improving performance.
Would You Try This Code?
Let me know in the comments! Also, feel free to share your experiences dealing with WordPress spam. 🚀