WordPress: Add a Custom Dashboard Widget
Add your own widget to the WordPress dashboard — show today's orders, a checklist, or quick links for your team.
Stop spam on old WordPress posts by closing comments automatically after a set number of days.
20 lines · Updated Nov 26, 2026
<?php
// Add to your theme's functions.php or a code-snippets plugin.
add_filter('comments_open', 'close_old_post_comments', 10, 2);
function close_old_post_comments($open, $post_id) {
$post = get_post($post_id);
if (! $post || $post->post_type !== 'post') {
return $open;
}
$days_old = (time() - strtotime($post->post_date)) / DAY_IN_SECONDS;
if ($days_old > 90) { // change 90 to your limit
return false;
}
return $open;
}
Old posts attract spam comments. A tutorial from three years ago rarely gets useful new discussion, but it does get links to casinos and fake shops — which you then have to moderate. This snippet automatically closes comments on posts older than a set number of days, while keeping existing comments visible.
The snippet hooks into WordPress's comment-open check. For each post, it compares the publish date with your cut-off. If the post is older, comments are reported as closed; newer posts keep comments open as normal. Nothing is changed in the database, so removing the snippet restores the original behaviour.
functions.php or a small custom plugin.WordPress also has a setting under Settings → Discussion: "Automatically close comments on posts older than X days". The snippet is useful when you want different rules per post type or category, or to manage the behaviour in code alongside your other customisations.
Will this delete existing comments? No. Existing approved comments stay visible.
Related: Limit post revisions · Log 404 errors
Add the code to your theme's functions.php or a code-snippets plugin. Change 90 to the number of days after which comments should close.
Share this snippet
Add your own widget to the WordPress dashboard — show today's orders, a checklist, or quick links for your team.
Log every 404 on your WordPress site so you can see which broken links visitors actually hit — and fix them.
Brand the WordPress login page with your own logo and link — a few lines of code, no plugin needed.