How I Stopped WordPress Comment?Spam

风枫姑娘_4104

风枫姑娘_4104

2016-06-06

1656人浏览

原创

Read the full article at: How I Stopped WordPress CommentSpam I love almost every part of being a tech blogger: ?learning, preaching, bantering, researching. ?The one part about blogging that I absolutely loathe: ?dealing with SPAM comment

Read the full article at: How I Stopped WordPress Comment Spam

Treehouse
Wufoo

I love almost every part of being a tech blogger: ?learning, preaching, bantering, researching. ?The one part about blogging that I absolutely loathe: ?dealing with SPAM comments. ?For the past two years, my blog has registered 8,000+ SPAM comments per day. ?PER DAY. ?Bloating my database with that trash slows down my blog in every which way, and recently I decided I was done with it. ?I was also tired of moderating comments and seeing loads of SPAM comment notifications?in my email inbox. ?Done. ?And like a boss…I stopped it. ?Dead. ?Here’s how I did it!

How I Was Getting Spammed

There’s no way to tell, but?I suspect bots detected that I had a WordPress blog, knew the form keys for submitting comments, and did so accordingly. ?I was getting comments for Viagra, Cialis, Michael?Kors, Nike shoes, and more. ?Stuff only bots would spend the time on. ?It all had to be a detected, automated attack — nothing targeted.

What Wasn’t Working

Everything. ?I had used different WordPress plugins and continued to get SPAM. ?Akismet is the premier WordPress SPAM protector and it?wasn’t able to prevent the problems — and included 100KB+ of JavaScript which slowed down my site. ?I never used a CAPTCHA utility because any barrier to commenting on my site should be a problem I deal with, not all of you. ?In the end, I was let down by numerous utilities. ?I was disappointed but refused to give in.

What Worked

The first step was removing all of the anti-spam plugins, as there was a good chance they were messing with each other and letting the SPAM in. ?My solution was allowing the generic anti-spam solution: ?adding an INPUT to the form which should?remain empty during the submission?process. ?Empty in value but present via key: ?the premise is that bots that read form inputs would?populate the form field values with rubbish just to make sure submissions weren’t rejected based on empty values.

How I Implemented Spam Protection

You can’t simply add inputs?on the server side — they are output to the page and the bot can read those and populate (or not populate) them. ?Creating those fields on the client side eliminates the simple bot / curl readers. ?You can add said form fields with JavaScript (via any framework) and that’s your best bet. ?Before we do that, however, let’s implement?the server-side SPAM block check.

Contact Form 7 WordPress Form Builder and Mail Integration
Contact Form 7 WordPress Form Builder and Mail Integration

Contact Form 7 是 Rock Lobster Inc. 公司长期运行的 WordPress 表单插件,拥有超过 1000 万次活跃安装,并在 contactform7.com 提供官方文档。此技能适用于需要了解网站表单、邮件模板、验证行为和 shortcode 位置在 Contact Form 7 中实际处理方式的代理。

下载

The WordPress PHP

Before accepting a comment on the server side, we need to check for the dynamic key presence. ?All we need is an isset check:

// Fuck off spammers
function preprocess_new_comment($commentdata) {
	if(!isset($_POST['is_legit'])) {
		die('You are bullshit');
	}
	return $commentdata;
}
if(function_exists('add_action')) {
	add_action('preprocess_comment', 'preprocess_new_comment');
}

If the check fails, we reject the comment. ?Of course this means that users without JavaScript support will?have their comments rejected, but the chance of being spammed is probably greater than that of users without JS support so I’m fine with that. ?If the key isn’t set, we outright reject the comment all together. ?This is a chance, of course, but statistics show it’s worth it.

The JavaScript

The easy answer here is using basic?JavaScript to inject the form field, and since every JS framework?has its own syntax, I’ll pass on providing code for all of them. ?Adding?said field upon page load seems a bit suspect to me, as an intelligent bot may be able to detect that. ?In the case of my blog, I use MooTools and submit comments via AJAX, so I simply append the secret field within the JavaScript code upon submission:

var form = $('comment-form');
new Request({
    url: form.action,
    method: 'post',
    onRequest: function() {},
    onSuccess: function(content) {},
    onComplete: function() {}
  }).send(form.toQueryString() + '&is_legit=1');
});

Adding that key?upon submission has proven safe to this point.

WINNING

After two weeks I’ve received 0 SPAM comments. ?Zero. ?None. ?Nil. ?Absolute zip. ?I went from over 8,000 per day to none. ?Better than Akismet, better than any plugin. ?Take WordPress SPAM prevention into your own hands — use client-side magic with a small PHP function to make your life easier!

相关文章

WPS零基础入门到精通全套教程!
WPS零基础入门到精通全套教程!

全网最新最细最实用WPS零基础入门到精通全套教程!带你真正掌握WPS办公! 内含Excel基础操作、函数设计、数据透视表等

下载

相关标签:

wordpress

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
GoFrame框架Web开发与路由中间件
GoFrame框架Web开发与路由中间件

本专题聚焦GoFrame框架的Web开发,拆解路由注册与匹配规则、请求参数获取、参数校验与中间件执行顺序,说明统一响应、JWT鉴权、文件上传与接口文档生成的落地方式,并补充优雅重启与测试实践。

2026.11.17

0

15

GoFrame框架安装与gf命令行工具
GoFrame框架安装与gf命令行工具

本专题围绕GoFrame框架的安装与工具链配置展开,讲清Go版本要求、gf命令行的安装方式、代理镜像与依赖准备,说明项目初始化、热编译、交叉编译打包的做法,并整理安装阶段的常见报错与验证步骤。

2026.11.17

0

15

GoFrame框架零基础入门
GoFrame框架零基础入门

本专题整理GoFrame框架的入门知识,从模块化设计理念、环境准备到跑通第一个Web服务,讲清目录分层、配置加载、日志与中间件的用法,并对比Gin、Go-Zero的差异,帮助Go开发者建立整体认知。

2026.11.17

0

15

SQLAlchemy 一对多关系怎么建立
SQLAlchemy 一对多关系怎么建立

SQLAlchemy 一对多关系怎么建立?本专题整理父子表外键、relationship 双向映射、关联查询、级联删除和加载优化等相关文章,帮助你理解并实现常见的 ORM 关系建模。

2026.11.17

0

10

SQLAlchemy 如何增删改查
SQLAlchemy 如何增删改查

SQLAlchemy 如何增删改查?本专题整理 ORM 新增记录、条件查询、更新和删除、Session 事务提交与批量处理等实用文章,帮助你掌握 Python 数据库操作流程。

2026.11.17

0

11

SQLAlchemy ORM 模型怎么写
SQLAlchemy ORM 模型怎么写

SQLAlchemy ORM 模型怎么写?本专题整理模型类定义、Mapped 字段映射、主键与约束、表结构创建和常见报错的相关文章,帮助你从基础配置到实际建模逐步掌握 SQLAlchemy。

2026.11.17

0

16

Go-Zero API开发使用教程
Go-Zero API开发使用教程

本专题聚焦Go-Zero的API开发,拆解.api描述文件的语法结构、路由与分组定义、参数绑定与校验规则,说明统一响应体、JWT鉴权、跨域与接口文档生成的落地方式,并给出网关聚合与接口测试的实践建议。

2026.11.12

160

15

Go-Zero安装与goctl环境配置教程
Go-Zero安装与goctl环境配置教程

本专题围绕Go-Zero的安装与工具链配置展开,讲清Go版本要求、goctl安装方式、代理镜像、protoc依赖与环境变量设置,说明多版本共存、升级回退与CI流水线安装的做法,并整理安装阶段的常见报错与验证步骤。

2026.11.12

120

15

Go-Zero零基础入门教程
Go-Zero零基础入门教程

本专题整理Go-Zero的入门知识,从框架定位、环境准备到跑通第一个API服务,讲清目录分层、配置加载、日志与中间件的基本用法,并对比Gin、Kratos的差异,帮助Go开发者建立整体认知,判断它是否适合当前项目。

2026.11.12

100

15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程