Show an Estimated Reading Time to Your WordPress Readers

James Parsons by James Parsons Updated Feb 2nd, 2024 2 Comments•

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!

Related Code Snippets

Written by James Parsons

James Parsons is the founder and CEO of Content Powered, a premier content marketing agency that leverages nearly two decades of his experience in content marketing to drive business growth. Renowned for founding and scaling multi-million dollar eCommerce businesses through strategic content marketing, James has become a trusted voice in the industry, sharing his insights in Search Engine Watch, Search Engine Journal, Forbes, Entrepreneur, Inc, and other leading publications. His background encompasses key roles across various agencies, contributing to the content strategies of major brands like eBay and Expedia. James's expertise spans SEO, conversion rate optimization, and effective content strategies, making him a pivotal figure in the industry.