Show an Estimated Reading Time to Your WordPress Readers

Updated 02/02/2024, Posted 02/02/2024 by James Parsons James Parsons 0 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

Hi, I'm James Parsons! I founded Content Powered, a content marketing agency where I partner with businesses to help them grow through strategic content. With nearly twenty years of SEO and content marketing experience, I've had the joy of helping companies connect with their audiences in meaningful ways. I started my journey by building and growing several successful eCommerce companies solely through content marketing, and I love to share what I've learned along the way. You'll find my thoughts and insights in publications like Search Engine Watch, Search Engine Journal, Forbes, Entrepreneur, and Inc, among others. I've been fortunate to work with wonderful clients ranging from growing businesses to Fortune 500 companies like eBay and Expedia, and helping them shape their content strategies. My focus is on creating optimized content that resonates and converts. I'd love to connect – the best way to contact me is by scheduling a call or by email.