How long will this take to read?

Are you looking to get better engagement with your blog? Apparently, if you let people know how long it takes to read the article that will actually help. Who knew?!So without any further a do, that’s what I’m going to help you with today.

Lets get to the math.

To calculate how long it takes to read an article we break it down into the following steps:

  • Get the total word count of your article

  • Divide the total word count by 200 to get a decimal number

  • Round the decimal number you are given up or down

  • voila! you have your minutes to read an article

OK, so that is how it works, so how about we look at how that might look as a PHP function.

function calculate_time_to_read($data) { $word_count = str_word_count($data); $decimal_number = $word_count / 200; $time_to_read = round($decimal_number, 0, PHP_ROUND_HALF_UP); return $time_to_read; }

We start by using the php function str_word_count to get the number of words in $data. When then move on to step two and divide that by 200, which gives us our decimal number. We then use the PHP function round to round up if our decimal number is 1.5 or higher, else it rounds down.

Cool, so lets look at how this function could now be used on a WordPress blog. You would first need to add the above function to your functions.php file in your theme folder. Once you have done that, you’ll just need to add the following snippet inside the WordPress loop.

echo "<p>".calculate_time_to_read(get_the_content())." mins read</p>";

Hey presto. You have successfully jazzed up your blog with a cool little feature. 🙌🏻

;