WordPress: Disable Comments on Old Posts Automatically
Stop spam on old WordPress posts by closing comments automatically after a set number of days.
Log every 404 on your WordPress site so you can see which broken links visitors actually hit — and fix them.
23 lines · Updated Nov 26, 2026
<?php
// Add to your theme's functions.php or a code-snippets plugin.
add_action('template_redirect', 'log_404_errors');
function log_404_errors() {
if (! is_404()) {
return;
}
$url = home_url(add_query_arg(null, null));
$referrer = wp_get_referer() ? wp_get_referer() : 'direct';
error_log(sprintf(
'[404] %s | from: %s',
$url,
$referrer
));
}
// View the log in wp-content/debug.log (with WP_DEBUG_LOG on),
// or in your hosting error log. Then add a redirect for the
// URLs that get hit most often.
Every 404 error is a visitor who wanted something and did not find it — often because of an old link, a changed slug or a typo in a menu. Search Console shows some of them days later; this snippet logs them as they happen. It records each 404 with the requested URL, the referring page and the time, so you can fix broken links and add redirects where they matter.
/wp-admin.php.bak or /.env: bots scanning for weaknesses — ignore them, but make sure those files really do not exist.Do 404 errors hurt SEO? 404s for pages that never existed are normal. Broken internal links and lost backlinks to removed pages are worth fixing with redirects.
Related: Dynamic sitemap · Custom dashboard widget
Add the code to your theme's functions.php or a code-snippets plugin. Turn on WP_DEBUG_LOG in wp-config.php to write to wp-content/debug.log, then check it weekly.
Share this snippet
Stop spam on old WordPress posts by closing comments automatically after a set number of days.
Add your own widget to the WordPress dashboard — show today's orders, a checklist, or quick links for your team.
Brand the WordPress login page with your own logo and link — a few lines of code, no plugin needed.