Show an Estimated Reading Time to Your WordPress Readers
This is a fun one. We will assume 200 words per minute, which is a conservative estimate for your average adult reading speed.
This one is two parts. First, add this to your theme's function.php file:
/*
* Estimated reading time calculation and shortcode
*/
function calculate_reading_time() {
$post_content = get_post_field('post_content', get_the_ID());
$word_count = str_word_count(strip_tags($post_content));
$reading_time = ceil($word_count / 200); // Assuming 200 words per minute, adjust as needed
return $reading_time;
}
function reading_time_shortcode() {
$reading_time = calculate_reading_time();
return $reading_time . ' minute' . ($reading_time == 1 ? '' : 's');
}
add_shortcode('reading_time', 'reading_time_shortcode');
function display_reading_time() {
$reading_time = calculate_reading_time();
echo $reading_time . ' minute' . ($reading_time == 1 ? '' : 's');
}
This calculates the reading time of your posts. It also creates a shortcode, so you can simply use the following shortcode on a page or a post if you want to display it on specific pages: [reading_time]
If you want to display it on multiple pages, let's proceed to step two.
To display this at the top of every blog post, find your single.php file next. Then, add this where you'd like this to appear:
<p> Reading time: <?php
if (function_exists('display_reading_time')) {
display_reading_time();
}
?></p>
Feel free to adjust the text and styling as needed.
You can also echo the shortcode we created:
<p>Reading time: <?php echo do_shortcode('[reading_time]');?></p>
Lastly, you can always create a widget, if your blog template or page has a sidebar or widget section that you'd like to add this to. Just add a "text" or "Custom HTML" widget and then your shortcode: [reading_time]
Personally, I like the single.php option where it's included in the theme. Find a nice-looking clock icon to add next to it and you're set!
September 18, 2024
Thanks for this tip James. It was really helpful to add the reading time! 😊
September 24, 2024
Hey Hobert!
You know it's always awesome to hear that someone found the post helpful. Adding reading time is pretty cool for making things interesting isn't it?
I usually like to experiment with different styles and formats to keep things fresh too. I hope your readers are loving the new feature!
If you have any more questions or need more tips just give me a shout!