Created
January 16, 2024 19:17
-
-
Save chuneycu2/cdb97fff3137f17f6749eb0f21a7250a to your computer and use it in GitHub Desktop.
Calculate the average reading time for a blog post
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
// Get the post content (within the Loop) | |
$copy = get_the_content(); | |
// Strip all HTML from the content | |
$cleaned = strip_tags($copy); | |
// Get the word count | |
$word_count = str_word_count($cleaned, 0); | |
// Set up string to use | |
$mins = ''; | |
// Calculate read time in min (based on average 230 WPM) | |
if (floor($word_count / 230) < 1) : $mins = '1'; else : $mins = floor($word_count / 230); endif; | |
$reading_time = $mins . ' min read'; | |
// echo $reading_time wherever you need it |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment