Skip to content

Instantly share code, notes, and snippets.

@chuneycu2
Created January 16, 2024 19:17
Show Gist options
  • Save chuneycu2/cdb97fff3137f17f6749eb0f21a7250a to your computer and use it in GitHub Desktop.
Save chuneycu2/cdb97fff3137f17f6749eb0f21a7250a to your computer and use it in GitHub Desktop.
Calculate the average reading time for a blog post
// 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