WordPress: Disable Comments on Old Posts Automatically
Stop spam on old WordPress posts by closing comments automatically after a set number of days.
Add an “X min read” badge to your WordPress posts — it counts words at 200 words per minute, with no plugin needed.
16 lines · Updated Nov 26, 2026
<?php
function penducoder_reading_time() {
$words = str_word_count( wp_strip_all_tags( get_the_content() ) );
$minutes = (int) ceil( $words / 200 );
return $minutes < 1 ? 1 : $minutes;
}
add_filter( 'the_content', function ( $content ) {
if ( ! is_single() || ! in_the_loop() || ! is_main_query() ) {
return $content;
}
$badge = sprintf(
'<p class="reading-time">%s min read</p>',
esc_html( penducoder_reading_time() )
);
return $badge . $content;
} );
A small "5 min read" label at the top of a post tells readers what they are committing to before they start. It is a proven way to reduce bounce on long tutorials, and most themes do not include it. This snippet calculates the estimated reading time from the post's word count and displays it automatically — no plugin, no database changes.
functions.php or a small custom plugin, so theme updates do not remove it.str_word_count() only counts Latin letters. Split on whitespace with preg_split('/\s+/u', $text) instead.Does reading time help SEO? Not directly, but clearer expectations can improve engagement, which is good for readers and for your site.
Related: Reading-progress bar without a plugin · 5 code snippets every developer needs
Add it to your child theme's functions.php or the Code Snippets plugin. It only runs on single post pages (is_single()), inside the main loop, so your homepage and archive pages are untouched. Style the badge by adding CSS for the .reading-time class in Appearance → Customize → Additional CSS, for example .reading-time { color: #666; font-size: 0.9em; }.
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.
Log every 404 on your WordPress site so you can see which broken links visitors actually hit — and fix them.