A WordPress plugin that filters contact-form and comment spam with a model I trained in Python and run in plain PHP. No CAPTCHA, no API key, and no message ever leaves the site.
Contact-form spam is now written by cheap language models: fluent, unique enquiries that pass CAPTCHAs, honeypots and keyword rules. The filters that can read them, like Akismet and CleanTalk, send every visitor's message to their own cloud and charge per check.
I wanted a filter that reads the message, runs on ordinary shared hosting, sends nothing off the site and never silently loses a real lead.
What I built
01
A quarantine instead of a delete button
Spam is held for 30 days, never deleted on arrival. Releasing a message delivers it exactly as it would have been, notification email included, and can add the sender to an allow list. If anything fails, such as a missing model file, the message is delivered.
02
Every verdict explains itself
Each held message shows its score, the threshold it crossed and the words that pushed it towards spam. A site owner can see why something was held before deciding to release it.
03
Trained in Python, served in plain PHP
A logistic regression on hashed word and character n-grams plus link and sender features, trained on 96,549 licence-checked rows. A custom featurizer uses crc32 hashing in both languages, and shared fixtures prove PHP and Python agree to four decimal places. PHP reads only the weights a message touches, straight from a 2.1 MB file.
04
Rules the owner controls
Allow and block lists for emails, domains, IPs and phrases run before the model. If a message matches both lists, allow wins.
How it fits together
Every check runs inside WordPress. The model is trained offline and ships as a file in the plugin.
Form or commentCF7, WPForms, comments
Allow and block listschecked first
Local model in PHP2.1 MB, 3.5 ms worst case
Deliveredunsure ones tagged
Quarantine30 days, one-click release
Decisions and trade-offs
A small linear model instead of a transformer
It has to run on cheap shared hosting with no extensions. A hashed logistic regression scores an 8 KB message in 3.5 ms (p95) with about 8 MB of extra memory, and CI fails the build past 20 ms or 16 MB.
Trade-off: less accuracy than the MiniLM model I also trained. That one is kept for an optional cloud second opinion on unsure messages.
Fail open, hold instead of delete
For a small business, a lost enquiry costs more than one spam email. Unsure messages are delivered with a [Possible spam] tag, errors deliver the message, and held mail waits 30 days.
Trade-off: some spam still reaches the inbox, tagged.
Evaluation that can't flatter itself
Splits are grouped by source and by synthetic template, so test styles are never seen in training, and synthetic text is reweighted so the generator's style can't become a spam signal.
Trade-off: lower headline numbers. They still come from public and synthetic data until real labelled traffic is collected.
Results
0.33%
of genuine messages held on the synthetic test set (95% CI 0.19 to 0.58%)
90.5%
of spam caught at that rate. Not yet measured on real contact-form traffic