
🚫 How to Block Spam Comments with Links on Your WordPress Site (No Coding Needed!)
Are spam comments with unwanted links filling up your WordPress website? It’s frustrating, unprofessional, and bad for SEO.
But don’t worry — you can block all comments containing links easily using a simple code snippet, without touching your theme files!
In this guide, you’ll learn how to:
- Stop spam comments that include links
- Show a popup alert when someone tries to submit a comment with a link
- Use a plugin called WPCode to safely add custom PHP code
🧰 What You Need
We’ll use a free and safe plugin called WPCode – Insert Headers and Footers + Custom Code Snippets – WordPress Code Manager to insert our code without editing functions.php
.
✅ Steps to Disable Comment Spam Links in WordPress
1️⃣ Install the WPCode Plugin
- Go to your WordPress Dashboard
- Navigate to Plugins → Add New
- Search for: WPCode – Insert Headers and Footers + Custom Code Snippets
- Click Install Now, then Activate
2️⃣ Add a New PHP Snippet
- After activating, go to Code Snippets → + Add Snippet
- Click on “Add Your Custom Code (New Snippet)”
3️⃣ Paste the Anti-Spam Code
Under the Code Type, choose PHP Snippet
Then paste this code:
phpCopyEditfunction block_comments_with_links_popup() {
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['comment'])) {
$comment = $_POST['comment'];
if (preg_match('/http(s)?:\/\/|www\./i', $comment)) {
echo '<script>alert("Links are not allowed in comments."); window.history.back();</script>';
exit;
}
}
}
add_action('init', 'block_comments_with_links_popup');
4️⃣ Name Your Snippet
Give your snippet a name, like “Block Comment Links”
5️⃣ Activate the Snippet
- On the right side, click “Active”
- Then click the Save Snippet / Update button
That’s it! Your WordPress site is now protected from spam comments that contain links.
🧪 How It Works
Whenever a user submits a comment that includes a link (like http://
, https://
, or www.
), they’ll see this popup message:
“Links are not allowed in comments.”
Their comment won’t be submitted, and they’ll stay on the same page.
🎯 Why This Method Is Better
- No need to edit
functions.php
- Safe, beginner-friendly
- Stops bots and human spammers who try to sneak links into your comment section
- Keeps your site clean and secure
💡 Bonus Tip
For even more spam protection, consider also installing:
- ✅ Akismet Anti-Spam
- ✅ Antispam Bee
- ✅ Disable Comments Plugin (if you don’t need comments at all)
📝 Final Words
Comment spam with links is annoying — but with WPCode and this simple snippet, you can stop it with just a few clicks. It’s easy, safe, and takes less than 5 minutes.
Keep your WordPress site clean, professional, and link-free today