Show an estimated reading time at the beginning of every blog post based on the content length. This gives readers a quick idea of how long the article will take to read and can improve engagement.
Steps
- Open your active theme’s
functions.phpfile or a custom functionality plugin. - Add the code below.
- Save the file.
- Visit any single blog post to see the reading time displayed above the content.
Benefits
- Displays an estimated reading time automatically.
- Improves user experience and engagement.
- Encourages readers to finish articles.
- Works for all blog posts.
- No plugin required.
/**
* Display an estimated reading time before post content.
*/
function wpfolks_add_reading_time( $content ) {
if ( is_singular( 'post' ) && in_the_loop() && is_main_query() ) {
$word_count = str_word_count( wp_strip_all_tags( $content ) );
$reading_time = max( 1, ceil( $word_count / 200 ) );
$notice = sprintf(
'<p class="wpfolks-reading-time"><strong>Reading Time:</strong> %d min read</p>',
absint( $reading_time )
);
$content = $notice . $content;
}
return $content;
}
add_filter( 'the_content', 'wpfolks_add_reading_time' );