Automatic Calculate estimated time of reading in Wordpress or PHP in one function & easy way

How to calculate estimate time of reading? Wordpress

Written By: Mohamed Atef

Published: 2021-12-19 | Comments: 1 | Category: 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 Date : 2022-02-25

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?

Leave a comment

Join us

Join our community and get the chance to solve your code issues & share your opinion with us

Sign up Now

Related posts

3 ways to use jQuery in your Wordpress website
Publish date: 2021-02-17 | Comments: 0

Tag: Wordpress

One effective way to increase your WordPress website speed
Publish date: 2021-04-02 | Comments: 2

Tag: Wordpress

How to add live chat in your Wordpress website?
Publish date: 2021-04-27 | Comments: 0

Tag: Wordpress

How to migrate Wordpress website from localhost to live site ?
Publish date: 2021-04-20 | Comments: 1

Tag: Wordpress

How to add reCAPTCHA to Wordpress login page?
Publish date: 2021-06-25 | Comments: 0

Tag: Wordpress