(Reading time: 6 – 10 minutes)
Tim Ferriss has a lot to say about blogging. In fact, he can talk for an hour straight about it, as I found out last year at WordCamp 2009 in San Francisco.
I learned about Tim’s strategy, and he gave us a few of his tactics and techniques. One of Tim’s really interesting tactics for treating readers with respect was providing an estimate of how much time it might take them to read an article. For those unacquainted with Tim’s writing, sometimes it’s long, 3000 words or more long.
These days, I’m starting to see a lot more blogs with an estimated reading time displayed. Back last June, a whole year ago, there weren’t very many. There are surely several ways to implement an estimated reading time; being WordPress, there is a plugin.
And so, once again, a Technical Tuesday, where we break down one of Website In A Weekend’s most popular plugins: Estimated Reading Time.
But…
Before we get started, (IMPORTANT →) You Do Not Need To Read The Code To Get The Point.
You do, however, need to scan over the code. Take it in as a “block.” Notice it’s not very long.
The Estimated Reading Time plugin
The Estimated Reading Time plugin is pretty short, just a couple of dozen lines. If this hurts your eyeballs, by all means jump right over the code.
Otherwise, check it out in all it’s PHP glory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | if (!function_exists('est_read_time')): function est_read_time( $return = false) { $wordcount = round(str_word_count(get_the_content()), -2); $minutes_fast = ceil($wordcount / 250); $minutes_slow = ceil($wordcount / 150); if ($wordcount <= 150) { $output = __("Reading time: < 1 minute"); } else { $output = sprintf(__("Reading time: %s - %s minutes"), $minutes_fast, $minutes_slow); } if ($return) { return '<p class="estread">' . $output . '</p>'; } else { echo '<p class="estread">' . $output . '</p>'; } } endif; if (!function_exists('est_the_content')): function est_the_content( $orig ) { // Prepend the reading time to the post content return est_read_time(true) . "\n\n" . $orig; } endif; if (function_exists('add_filter')): // Set this to priority 9 so it's called before wptextuarize/wpautop/etc add_filter('the_content', 'est_the_content', 9); endif; |
That’s not bad, 30 lines of pure D elegance.
Just for fun, let’s break it down… (no? Go for the goodies?)
The break down
1 2 | if (!function_exists('est_read_time')): function est_read_time( $return = false) { |
Let’s check to see if this function for estimated reading time already exists in WordPress, perhaps in another plugin. If so, skip over it.
If not, do some computation.
3 4 5 | $wordcount = round(str_word_count(get_the_content()), -2); $minutes_fast = ceil($wordcount / 250); $minutes_slow = ceil($wordcount / 150); |
These simple computations are the heart of the plugin, and if you’ve noticed the “2-2″ bug, this computation is where that occurs. Here’s how it works:
- After counting the words with
str_word_count, the PHProundfunction with argument -2 multiplies the result by 100. - Then, dividing by 250 and 150, respectively, gives a range, and the
ceilfunction returns the next highest integer from the division.
Not hard, right?
Let’s figure out what to show the reader:
7 8 9 10 11 | if ($wordcount <= 150) { $output = __("Reading time: < 1 minute"); } else { $output = sprintf(__("Reading time: %s - %s minutes"), $minutes_fast, $minutes_slow); } |
Here’s the other half of the “2-2 minutes” bug. Fixing it won’t be hard, will just take a little time examining how round and ceil work, or kludging in a fix by checking to see if $minutes_fast equals $minutes_slow.
Once we know what we want to display…
12 13 14 15 16 17 18 | if ($return) { return '<p class="estread">' . $output . '</p>'; } else { echo '<p class="estread">' . $output . '</p>'; } } endif; |
This means that when the function is called from some other part of WordPress, it will return a string to wherever it’s wanted. Else, it prints the estimated reading time string straight to your screen.
Note that “estread” class is added automatically. This CSS currently has to be added manually to your style sheet. (custom.css if you use Thesis) In the near future, the plugin will have it’s own style sheet.
Here’s where we set up for the hook…
12 13 14 15 16 17 | if (!function_exists('est_the_content')): function est_the_content( $orig ) { // Prepend the reading time to the post content return est_read_time(true) . "\n\n" . $orig; } endif; |
Calling est_reading_time with true. We want the string back because…
12 13 14 15 | if (function_exists('add_filter')): // Set this to priority 9 so it's called before wptextuarize/wpautop/etc add_filter('the_content', 'est_the_content', 9); endif; |
…we’re adding the string with the reading time directly to the content.
Easy, right?
“But why,” you might ask, “should I bother myself with these tedious details?”
That’s an excellent question, and you, Website In A Weekend reader, deserve an answer.
Ripping back the (plugin) veil
Long time readers (bofem) recall that I’ve published similar code and plugin breakdowns in the past. Just last week, we tore into the SEO Slugs plugin. A month or two past, we had the world’s shortest plugin.
Stay with me here…
If you think I might demonstrating something (more than just showing off even though I am sort of showing off, at least a little bit), you might be right.
I’m setting the stage for a nice big twist of Blogistan’s collective nose. Remember this old saw “Too many plugins is bad and makes your blog slow.”?
Who says so? Why do they say it? How do they know, who granted their authority and why should I believe what J. Random Blogger says about plugins, loading speed, or any other damn thing about blogging?
More important, why should you believe it?
Rule of thumb: If it didn’t come directly from a WordPress core developer, or plugin author with years of experience, or a professional webmaster, take it with a grain of salt. Maybe a big lump of salt.
Worse yet, even if you get your information straight from the horse’s mouth (so to speak), it may be suspect. I’ll go deeper into “why” in the future.
Let’s wrap this up.
Do you need estimated reading time?
This plugin is pretty small, and it’s not going to put much of a load on your WordPress installation (almost none, really), so using it won’t cost much more than installing it. But before you “just install it,” note that my primary reason for using it is because the length of my articles varies wildly between roughly 1 minute reading time to over 20 minutes reading time.
Further, the topics that I write on vary between highly accessible to highly technical.
Because of this wide range of length and topical difficulty in my blog posts, letting the reader know what’s in store just seems polite. And it works. Readers have commented that having some notion of their commitment ahead of time is very helpful.
Seeing a short post (1 – 2 minutes) may bring a reader in who might otherwise pass. Seeing a long post, a reader might choose to go get a cup of coffee before starting to read (I say this because this is what I’ve been told by a reader in the past.)
Displaying an estimated reading may or may not be useful for you. If you write articles of uniform length, your readers always know what to expect, reading time is superfluous. Likewise, if your articles are fairly well-focused and written in a uniform voice, your articles can be any length, from 100 to 3000 words, and your readers will be there for you regardless of article length.
Also. Remember why I do this stuff: Talent is Overrated, I write to learn, and deliberate practice is the only sure path to mastery.


I always found that one pretty helpful… here’s a question for you though.
What if, say, someone was to take the best parts of a variety of plugins and create a theme for themselves with them already built in? Asking in a hypothetical sort of way, but what would that do to the page load time etc?
Actually I’m more interested on if it’d be considered fairplay. :)
In general, load fewer files that do less work.
The problem with fewer files is that they’re longer, thus harder to work with.
.-= Dave Doolin´s last blog ..Blog Better with a Virtual Assistant: Tip #1 =-.
Okie doke just wondered – thanks.
Now off to deal with this mess my hacker caused *sigh*
Glad you added that little snippet about not needing to read the code but just scan it as a block. I still get a little twitchy when I see code :) This was interesting to me though because I’ve been seeing those reading times popping up a lot on blogs and wondered how that came about.
.-= Jean Sarauer´s last blog ..The True Story Behind Virgin Blogger Note’s New Look =-.
The important thing to see is that it’s *short.* And for that you don’t need to read a line of it.
.-= Dave Doolin´s last blog ..If You Want to Be a Better Blogger, Write Better =-.
I see your point about bringing new readers in with a short post, but I wonder how many you lose because of the long post. I know when I see something that takes a long time to read, I don’t bother.
.-= Mike Roosa´s last blog ..Entering A DoFollow Zone =-.
Mike, people who won’t read a long post are not in my audience, and I absolutely do not worry about that. I have found – from personal experience – that the length of an article is irrelevant to engagement. What matters the quality of the writing. I realize this runs exactly 180 degrees against the received opinion. But I’ll take experience over opinion any day.
.-= Dave Doolin´s last blog ..SEO for Writers and Artists (or, how to date your search engine) =-.
Fair enough. Quality is important, but for me, I just prefer shorter posts for reading because I have 100 different things I’m trying to do at once. Of course I prefer short quality posts. Nothing worse than reading a long post with poor quality.
.-= Mike Roosa´s last blog ..Making The First Dollar =-.
Good lord, I miss coding. I sucked at it and had my now ex holding my hand the whole way, but man it was fun to see the program do what I wanted it to do!
Okay, that aside, I think giving the reader an estimate of the time to read an article is an excellent idea. Then they know if they have time to read it right now, or if they need to come back later (or skip it all together :()
.-= Gurl´s last blog ..When Is Blogger’s Block not an actual Block? =-.
I’ve found that people more interested in skipping articles aren’t going to be interested in what I have to say anyway.
I tend to average between 800 – 1200 words, and it works really well for me. Venkat over at Ribbonfarm runs 2000-3000 words, and he certainly isn’t suffering for either readers or reader engagement. Neither is Tim Ferriss. Or Steve Pavlina.
On the other hand, Seth Godin gets read not because because his articles are short. He gets read because his articles are brilliant. People confuse these points: “Seth Godin writes short articles and gets lots of traffic, so my article should be short too, so I will get lots of traffic.”
Not hardly.
I totally feel you on the “people more interested in skipping articles aren’t going to be interested in what I have to say anyway.” I often find it difficult to keep things short and scanable. I am getting a bit better with that, but some posts just don’t work with that model.
.-= Gurl´s last blog ..Why Blogging Scares Me =-.
I don’t use this plug-in on my main site (it is on my writing site), but do appreciate it when I see it on others’ sites. I may not have time to read a 20 minute post right now, but do want to read it. This lets me know to walk away now, but come back later. And I do come back later. So, thanks for thinking of me, Dave. ;)
.-= Anne Bender´s last blog ..Uh, Excuse Me? =-.
Anne, you bet. Deacon is the person that originally told me the “get a cup of coffee ” story. I figured others would have the same inclination.
.-= Dave Doolin´s last blog ..If You Want to Be a Better Blogger, Write Better =-.
We may need the cup of coffee to uncross our eyes as we try to makes heads or tails out of all of that lovely code you share.
Thanks for the clarification about ‘too many’ plugins. It only gives me another questions. How do I learn who is a core programmer with years of experience and who isn’t?
I wonder how the plugin can decide if a post is dense and meaty or light and frothy, thus affecting the amount of time required to read. I know that I don’t pay much attention to the reading time when deciding to read more. It is more in the title and what I know about the author. Do you have a plugin to write me a catchy title for my posts?
.-= Ralph´s last blog ..Wake up and smell the – Kniphofia =-.
“Core programmer” two ways: 1. follow the wp-hackers mailing list, or 2. read the commit log on the source code repository.
In this case, it’s a simple wordcount algorithm. There are other statistics that could be used, Gunning Fog for example. I wrote an article on these a while back: http://website-in-a-weekend.net/creating-content/using-the-fd-word-statistics-plugin-to-sharpen-your-writing/
.-= Dave Doolin´s last blog ..Blog Better with a Virtual Assistant: Tip #1 =-.
You make it sound so easy. wp-hackers mailing list? source code repository? stand on my head?
.-= Ralph´s last blog ..50’s Nostalgia-1953 Studebaker =-.
He didn’t say Simon Says ‘stand on your head’. Back to the beginning for you. :)
Standing on head? Pictures… I want pictures!
Haven’t done it for a while. Hard to take a picture while doing it.
.-= Ralph´s last blog ..Sunday Funnies – Running on Water =-.
I think my blog don’t demand it, if it I will surely add it.
.-= Arafat Hossain Piyada´s last blog ..iPhone 3GS now $97 at Walmart, I need one =-.
Exactly. Use it when you need, not “just because.”
I’d like to see you take your work to a place where you just might need it.
You would get more clickthroughs from here on your web security link.
.-= Dave Doolin´s last blog ..SEO for Writers and Artists (or, how to date your search engine) =-.
I tore into this plugin a while back after seeing it on your site and Tim’s site.
The change I made was to average the minutes and give a more precise estimated reading time instead of a range. No real reason except “just because” :)
As with writing to learn, you can also code to learn. Pulling plugins apart is the best way to learn the gizzards of WordPress imho.
.-= Josh Kohlbach´s last blog ..Back Woes, Book Reviews, and Tuantuan Sleeping Bags =-.
Heh, I just read the comment above mine and laughed out loud when I realised you used “just because” in the exact opposite way.
I was of course referring to the “why bother taking it apart to make such a small change”, not the “why should I use this” question :)
.-= Josh Kohlbach´s last blog ..53 New Spam Comments.. Phew, That’s Good! =-.
I love doing stuff “just because” and wish I had a bit more time for that!
.-= Dave Doolin´s last blog ..Blog Better with a Virtual Assistant: Tip #1 =-.