How to Add Reading Time to Your Blog Post

This article is a 3 minute read

If you’re familiar with the blogging platform Medium, you’ve probably seen one of my favourite features. It’s the approximate reading time of the given article that you’re currently viewing.

 

medium

I recently included this in my site, so let’s take a look at how to implement it in WordPress (and really in any PHP based CMS).

I also have to credit Wes Bos, because it was during my Mentorship alongside him that I got to iron this out.

Step 1: You can make this into a function to store in your functions.php file, or you can code it directly into your page template. I chose the route of coding it directly into my page template because I was really only using it in one place. So, go ahead and open up your page template in your chosen text editor – in most cases this will be the single.php file.

single

Step 2: Take your existing loop (for this demo we’ll use a very basic loop), and grab the total word count within the loop

<?php 
if ( have_posts() ) {
	while ( have_posts() ) {
		the_post(); 
		//
                // get the content and store it in a variable
                $words = get_the_content();
                // 
		the_title();
		the_content();
	} // end while
} // end if
?>

Step 3: Get the word count of the total content

<?php 
if ( have_posts() ) {
	while ( have_posts() ) {
		the_post(); 
		//
                // get the content and store it in a variable
                $words = get_the_content();
                // get the word count of the content
                $wordCount = str_word_count($words);
		the_title();
		the_content();
	} // end while
} // end if
?>

Step 4: Get the amount of minutes by taking the total word count and dividing it by the approximate words per minute for the average human. Then round that to the closest minute.

<?php 
if ( have_posts() ) {
	while ( have_posts() ) {
		the_post(); 
		//
                // get the content and store it in a variable
                $words = get_the_content();
                // get the word count of the content
                $wordCount = str_word_count($words);
                // get minute count
                $minutes =  round($wordCount / 250);
		the_title();
		the_content();
	} // end while
} // end if
?>

Step 5: Now the total time in minutes is stored in the variable $minutes, so we need to pull it into a string of text, and echo it out to the page. However, we want to avoid unnatural language if the total read time is less than a minute (because it may be rounded to zero). For this we’ll put together an ‘if statement’.

Step 6: Create a span or paragraph tag for where you want to print out the resulting read time. This will probably be under the title tag

<span class="time-to-read">
</span>

Step 7: putting our ‘if statement’ inside the span to echo the correct phrase depending on the time in minutes returned

<span class="time-to-read">
 <?php if ($minutes <= 1) {
    // if the time to read is less than a one minute, 
    // echo the following phrase
    echo "This article is less than a 1 minute read";
    // If the article is more than a one minute read, 
    // echo the amount of minutes in the phrase below.
    } else {
    echo "This article is a " . $minutes . " minute read";
 }?>
 </span>

That’s it! Feel free to rearrange things and store them in a neat little function that you can call at will, but this is the basic idea. I’m always open to feedback, so if you have ways I can improve it, let me know in the comments.

Here is the final completed product in the loop:

<?php 
if ( have_posts() ) {
	while ( have_posts() ) {
	   the_post(); 
		//
                // get the content and store it in a variable
                $words = get_the_content();
                // get the word count of the content
                $wordCount = str_word_count($words);
                // get minute count
                $minutes =  round($wordCount / 250);
		the_title();
                  <span class="time-to-read">
                  <?php if ($minutes <= 1) {
                    // if the time to read is less than a one minute, 
                    // echo the following phrase
                    echo "This article is less than a 1 minute read";
                    // If the article is more than a one minute read, 
                    // echo the amount of minutes in the phrase below.
                    } else {
                    echo "This article is a " . $minutes . " minute read";
                  }?>
                  </span>
               the_content(); 
} // end while } // end if ?>

If you like it, share it!

Love this post?
Sign up for updates!

Join the Conversation, Leave a Reply

Your email address will not be published. Required fields are marked *