A few emails started landing in our admin inbox: a new comment was waiting for moderation. The odd part was that the site had no discussion turned on anywhere we’d set it up.
It kept happening for a couple of weeks, and it bothered me. Spam that gets in somewhere you didn’t expect is usually a sign of a gap, and I wanted to be sure the site was safe. It wasn’t something the client needed to deal with, so we tracked it down and closed it ourselves.
If you’re seeing the same thing, here’s why it happens and how to stop it for good.
Why it happens
Turning comments off in WordPress doesn’t switch them off everywhere. Two gaps catch most people.
The first is image pages. WordPress creates a separate page for every image you upload, at an address like ?attachment_id=123. Those pages keep their own comment settings, and they’re the usual source of the “why are spam comments landing on my images?” question.
The second is existing content. The setting in Settings → Discussion only applies to posts and pages created after you change it. Anything already published keeps whatever it had.
If your site doesn’t need comments, turn them off everywhere
Start with one question: does this site need comments at all? Most business, portfolio and corporate sites don’t. If yours doesn’t, switching comments off sitewide is faster and more reliable than adjusting settings one by one.
Add this to your child theme’s functions.php. It closes comments and pings on the front end, hides any existing comments, and removes the comment screens from the dashboard:
// Fully disable comments sitewide
add_action('admin_init', function () {
global $pagenow;
if ($pagenow === 'edit-comments.php') {
wp_redirect(admin_url());
exit;
}
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
foreach (get_post_types() as $post_type) {
if (post_type_supports($post_type, 'comments')) {
remove_post_type_support($post_type, 'comments');
remove_post_type_support($post_type, 'trackbacks');
}
}
});
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);
add_filter('comments_array', '__return_empty_array', 10, 2);
add_action('admin_menu', function () {
remove_menu_page('edit-comments.php');
});
add_action('init', function () {
if (is_admin_bar_showing()) {
remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
}
});
Use a child theme, not the main theme, so a theme update doesn’t wipe it out.
If you do want comments, close the gaps
When a blog genuinely needs comments, keep them on but lock down everything around them.
- Tighten the Discussion settings. In Settings → Discussion, require manual approval, require a previously approved comment from the author, and hold comments with two or more links for moderation. Add a list of known spam words and domains under Disallowed Comment Keys.
- Close comments on image pages. Add this to your child theme’s
functions.php:
// Disable comments on all attachment (image) pages
add_filter('comments_open', function ($open, $post_id) {
return get_post_type($post_id) === 'attachment' ? false : $open;
}, 20, 2);
add_filter('pings_open', function ($open, $post_id) {
return get_post_type($post_id) === 'attachment' ? false : $open;
}, 20, 2);
- Close comments on what’s already published. In Posts, select everything, choose Bulk Edit and set Comments to Off. Do the same for Pages.
- Add a filter. Akismet cuts spam volume the most, and its free tier covers most small sites. If you’d rather avoid Google reCAPTCHA for privacy reasons, a lightweight honeypot plugin works well.
- Turn off XML-RPC if you don’t use it. It isn’t about comments, but it’s another common target for spam and login attacks, so it belongs in the same pass. Leave it on if you use Jetpack or the WordPress mobile app.
Make it part of every launch
The real fix is never having to do this again. Put the right snippet in your starter child theme so every new site gets it automatically. Then add one line to your pre-launch checks: open an image’s attachment page and a blog post, and confirm neither shows a comment form.
It takes about ten minutes per site, and it saves weeks of moderation emails nobody should have to read.
Want us to look at your site?
Tell us where you are and where you want to be. We reply within one business day.
Written by
John Go
President and CEO of Webiato. Building and caring for websites for over a decade.