Last active
April 9, 2020 10:37
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can change 200 with the current standard of 400 words