How to Add Reading time in WordPress without using plugin

Reading Time in WordPress

When you get a visitor to your site, most probably you want them to stay on your website as long as possible and information that you will give them that will help is worth the effort. on many websites, Blogs, and Posts we have noticed that some sites providing an estimated reading time for articles. Some are as low as one minute while others are big posts. However, as I fount on my research, the technique of how to implement reading estimated time and some people have reported an increase in the number of time visitors to stay on their site.

If you run a WordPress blog, you are probably trying to find many certain plugins you can install to accomplish something similar, but I like to do as much as possible myself, and throwing this together is not to difficult. in order to keep your visitors to read your posts without clicking away.

Essentially, this takes each entry, strips all the code tags and extraneous text, and gets a word count. Then it divides by 200, which is a pretty average reading speed and rounds that to the next highest number.

Use this Code on your theme folder of function.php

According to Medium, people read about 200 words per minute. Medium also adds 12 seconds for each inline image, but I didn’t get that fancy.
//estimated reading time
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;
}

Then you need to call this function to display on your site, and you can put it anywhere, use something like this:

<?php echo reading_time(); ?>

This returns just a number, so you’ll have to add any context around it in your HTML to put it in context.

We hope that you found this article to be helpful. If you liked it, please feel free to check out some of these articles as well!
Click here.

Written by

Website Developer, I have worked on small to large Projects With Experience of 3yrs in Website Design & Development, ECommerce Development, etc.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store