If you are a blogger like me, it is likely that you use WordPress as your platform and you probably want visitors to stick around as long as possible. One way to do this is by providing an estimated reading time for your articles. You might have noticed that some sites do this, with some articles taking as little as one minute to read and others taking much longer.
From what I’ve found in my research, using an estimated reading time can help keep visitors engaged and on your site longer. And if you’re running a WordPress blog, you might be wondering how you can add this feature to your site.
Now, there are plenty of plugins you can use to accomplish this, but I prefer to do things myself whenever possible. And it’s actually not too difficult to add this feature to your site. Essentially all you need to do is take each post, strip out any code tags and unnecessary text and get a word count. Then, you divide that number by 200 (which is a pretty average reading speed) and round up to the nearest whole number. And that’s it!
Of course, you can always adjust the number of words per minute to suit your needs or try different methods of estimating reading time. But this is a good starting point to keep your visitors reading your posts without clicking away. It’s like giving them a heads up!
WordPress Reading Time PHP Code
As mentioned, the average person can read around 200 words per minute. But, if you add in some inline images, it’s a good idea to give an extra 12 seconds for people to take it all in. I didn’t get too fancy with it, but it’s something to keep in mind. You can add this code in your theme’s functions.php file:
function reading_time() { $content = get_post_field( 'post_content', $post->ID ); $word_count = str_word_count( strip_tags( $content ) ); $readingtime = ceil($word_count / 200); if ($readingtime == 1) { $timer = " minute"; } else { $timer = " minutes"; } $totalreadingtime = $readingtime . $timer; return $totalreadingtime; }
How To Display the PHP Code
To display the function on your site, you can call it and place it anywhere using code:
php echo reading_time(); >
This shows just a number and you may need to tweak it to your specific needs just like what I did. I will be placing a link on the comment section where you can copy/paste it the codes.
I hope this will enhance the aesthetic and functionality of your blog, because it sure did for me. If you found this post informative and beneficial, please consider sharing it with other bloggers.
September 2023 brief update
I have recently made some changes to the link in the comment section. You can now effortlessly copy and paste both the PHP code and the display code with the fresh link. Why the change, you wonder? Well, the trusty code plugin was misbehaving and just in case it happens again the code is readily available. Plus, that old link had its expiry date. But guess what? I’ve fixed it up, and it’s smooth sailing now!