search ]

Display the Estimated Post Reading Time on WordPress

You’ve might come across certain websites that provide the estimated reading time of an article like this one. Personally, when I saw this, I thought it was a bit absurd.

However, I’ve already come across several articles explaining that displaying the estimated reading time can increases engagement with the content.

It has been found that users who know the time they need to invest in reading an article tend to read more of the same article, and their time spent on the site increases by about 10%. I refrained from explaining why, but it seems to be simple psychology at work.

It turns out that not long ago, a startup company called monday.com (I developed their first blog) asked me to embed this option, so I’m even considering adding this functionality to this blog.

Displaying the Estimated Reading Time

If you want to display the estimated reading time in your articles, there is a nice and simple plugin that does the job called Reading Time WP. You can install it and add the functionality automatically using shortcodes, but it does not support translation to Hebrew.

So here’s a simple function I created that displays the estimated reading time in Hebrew. Add this function to your functions.php file of your child theme and call the function wherever you want in your template files.

/**
 * Estimate time to read the article
 *
 * @return string
 */
function savvy_estimated_reading_time() {

	$post = get_post();

    $words = count(preg_split('/\s+/', strip_tags( $post->post_content )));
	$minutes = floor( $words / 200 );
	$seconds = floor( $words % 200 / ( 200 / 60 ) );

	if ( 1 <= $minutes ) {
		$estimated_time = ($minutes == 1 ? ' One Minute' : $minutes . 'Minutes ) . ' &' . ($seconds == 1 ? 'One Second :  $seconds . ' Seconds );
	} else {
		$estimated_time = $seconds . ' Seconds;
	}

	return $estimated_time;

}
<?php echo savvy_estimated_reading_time(); ?>

Note that we used the php function named strip_tags() to remove html and php tags from the word count calculation.

How’s the calculation done?

According to quick studies, an adult’s reading speed is about 250 words per minute. Since I don’t want to display pessimistic reading times, I used a reading speed of 200 words per minute in the function, even though most readers do it faster.

Also, in my opinion, reading time depends on the type of content you are reading. If you need to analyze code or refer to graphs and others, it is clear that the reading time will be longer.

0 Comments...

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!

Savvy WordPress Development