Written By: Mohamed Atef
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
Join our community and get the chance to solve your code issues & share your opinion with us
Sign up Now