Ever since Medium popularized the concept of the read times on articles, I’ve been seeing it as a design element in more WordPress projects.
Getting a great estimate for average read time is complicated, but if you just need a blunt tool for calculating it, you can use:
amount of words in the post / average reading speed
According to Medium, people read about 275 words per minute. Medium also adds 12 seconds for each inline image, but I didn’t get that fancy.
Here’s the snippet:
|
<?php |
|
/** |
|
* Estimates the reading time for a given piece of $content. |
|
* |
|
* @param string $content Content to calculate read time for. |
|
* @param int $wpm Estimated words per minute of reader. |
|
* |
|
* @returns int $time Esimated reading time. |
|
*/ |
|
function prefix_estimated_reading_time( $content = '', $wpm = 300 ) { |
|
$clean_content = strip_shortcodes( $content ); |
|
$clean_content = strip_tags( $clean_content ); |
|
$word_count = str_word_count( $clean_content ); |
|
$time = ceil( $word_count / $wpm ); |
|
return $time; |
|
} |
To output the read time in your post, use something like this:
|
<div class="reading-time"> |
|
<?php echo prefix_estimated_reading_time( get_the_content() ); ?> min read |
|
</div> |
If you prefer to use a plugin, there’s a few around. This snippet is mostly borrowed code from Reading Time WP, which is also on GitHub.