Stop Spam From Your Contact Form (HTML, PHP and WordPress)
Bots fill your form with junk. Add a hidden field, server-side checks, rate limits and a CAPTCHA if needed.
Spam bots find forms automatically. A few layers stop most of them without annoying real visitors.
Layer 1: A honeypot field
Add a field that people cannot see but bots fill in. If it has a value, ignore the submission.
<div style="position:absolute;left:-9999px" aria-hidden="true">
<label>Leave this empty <input type="text" name="website" tabindex="-1" autocomplete="off"></label>
</div>
On the server, in PHP:
if (!empty($_POST['website'])) { http_response_code(200); exit; }
Layer 2: Check on the server
Never trust the browser alone. In PHP, validate the email with filter_var($email, FILTER_VALIDATE_EMAIL), limit message length, and reject messages with many links.
Layer 3: Slow the bots
Record when the form was loaded and reject submissions made in under 2 to 3 seconds. Limit how many messages one address can send in a minute.
Layer 4: A CAPTCHA if the spam continues
Services such as Google reCAPTCHA, hCaptcha and Cloudflare Turnstile verify the visitor. Add the widget to the form and verify the token on the server, since a check done only in the browser is useless. Make sure to mention the service in your privacy policy.
Layer 5: Protect the mailbox
Send the message by authenticated SMTP, not mail(). See our PHP form guide. Do not put raw visitor input into email headers.
On WordPress
Use the spam options of your form plugin, and an anti-spam plugin. See our spam comment guide.
Related guides
- WordPress User Can't See Posts They Created: Roles and Permissions
- WordPress Site Health Warnings: What To Fix First
- WordPress: Create a New Admin User via Database When Locked Out
- WordPress Automatic Updates: Enable, Disable and Safe Settings