Skip to content

Instantly share code, notes, and snippets.

@wpgaurav
Last active April 9, 2020 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpgaurav/8312b81a7094b6827b21040ac5d7c2c2 to your computer and use it in GitHub Desktop.
Save wpgaurav/8312b81a7094b6827b21040ac5d7c2c2 to your computer and use it in GitHub Desktop.
Want to tell your readers how long it will take an article to read completely? Insert the following code into the right file (e.g., single.php or page.php) in the right place. Visit https://gauravtiwari.org/code/ for more
<?php
// Insert the following code into the right file (e.g., single.php or page.php) in the right place (above post meta and below post title) in your theme. It counts total words in an article and calculates time by estimating that on average a reader can read 200 words in a minute.
$mycontent = $post->post_content;
$word = str_word_count(strip_tags($mycontent));
$m = floor($word / 200);
// considering 200 words per minute is human ready capacity average
$s = floor($word % 200 / (200 / 60));
$est = $m . ' minute' . ($m == 1 ? '' : 's') . ', ' . $s . ' second' . ($s == 1 ? '' : 's');
?>
@wpgaurav
Copy link
Author

wpgaurav commented Apr 9, 2020

You can change 200 with the current standard of 400 words

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment