user

Mohamed Atef

19 Dec 2021

How to calculate estimate time of reading? Wordpress

Wordpress

Show your visitors the estimated time of reading for each article in your blog in Wordpress or PHP, 
I am going to show you a simple way to calculate the estimated time of reading for each article on your website/blog,
in the function below we gonna calculate 50 words per minute 

function site_estimated_reading_time( $content = '', $wpm = 50 ) {
 $clean_content = strip_tags( $content );
 $word_count = str_word_count( $clean_content );
 $time = ceil( $word_count / $wpm );
 if($time > 1){
 return $time.' Minutes';
 }else{
 return 'Less than a Minute';
 }
}

and to calculate it in your custom post type you can pass the content of the post to the function like 

<?php echo "<span class='reading_time'>Estimated reading time: ".site_estimated_reading_time(get_the_content())."</span>"; ?>

and for PHP you can pass any text and it will return it into minutes,
Thank you for reading, if you have any questions please let me know in the comments below

 

 

Comments

Shilpa

25 Feb 2022

github

This is really amazing, I should use the same logic for my WordPress website posts. Thanks for sharing it.

I have only one concern, I believe normal kids of age 12-15 years old - can read 80-100 words per minute at least. So I must use it like,

function site_estimated_reading_time( $content = '', $wpm = 90) {
 $clean_content = strip_tags( $content );
 $word_count = str_word_count( $clean_content );
 $time = ceil( $word_count / $wpm );
 if($time > 1){
 return $time.' Minutes';
 }else{
 return 'Less than a Minute';
 }
}

The above code snippet is taken from Mohamed Atef's Article.

If the ratio needs to be counted to 50 WPM, it must be for average typing speed. Right?

© 2024 Copyrights reserved for web-brackets.com