1. Home
  2. /
  3. Web Design & Development
  4. /
  5. WordPress
  6. /
  7. How to calculate read time of an article or textual content? And, implement it on a Website or WordPress site?

How to calculate read time of an article or textual content? And, implement it on a Website or WordPress site?

how-to-calculate-read-time

Introduction

In this example, we are going to learn how to implement read time calculation of an article or blog and implement it on a website or WordPress site.

Displaying the Read time on an article, add values to the user experience. It helps to make the user aware of the time it may take if they continue reading your content.

How to Calculate Read Time of an article or content?

Add the below piece of code in WordPress functions.php or your common functions file. At present, we are considering a read time of 180 words per minute. You can change the number as per your need.

<?php 

if (!function_exists('getArticleReadTime')):
	function getArticleReadTime($text){

		/*PHP Round fractions up so the sample text post minimum read time is 1 minute*/
		$readTime = ceil(str_word_count($text)/180);
		if ($readTime == 1){
			$readTime = $readTime . ' Min read';
		}else{
			$readTime = $readTime . ' Mins read';
		}
		return $readTime;
	}
endif;

?>

How to display read time on the frontend?

Now, to display the read time on the frontend, call the function getArticleReadTime() and pass the content as an argument. Here is an example of how to do so.

<?php 
$content = 'This is the text for which we need to calculate the read time.';
echo getArticleReadTime( $content );
?>

In WordPress, we can use the below piece of code to get the read time of the content fetched through get_the_content function.

<?php 
$content = get_the_content();
echo getArticleReadTime( $content );
?>

If you think you’re getting more read time than it should be. You should consider stripping all the html tags and make sure you’re supplying only plain text to getArticleReadTime() function. Here is the example of the same.

<?php 
$content = wp_strip_all_tags( get_the_content() );
echo getArticleReadTime( $content );
?>

Thats it! You’re done 🙂

Reviews & Ratings Get your stoe online with Shopify in 60 minutes Shop Now