Created
March 10, 2025 15:42
-
-
Save BrendonKoz/37a24546bc8a2c35ee4ae2f711165eb4 to your computer and use it in GitHub Desktop.
Calculate reading time for ProcessWire blog/news 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
<?php // site/ready.php | |
// Blog/News Post Page - set reading time | |
$wire->addHookBefore('Pages::saveReady(template=news-post)', function(HookEvent $event) { | |
// Get values of arguments sent to hook (and optionally modify them) | |
$page = $event->arguments(0); | |
// Get the word count of all available text fields that make up the content of the page | |
$count['summary_word_count'] = count(wire('sanitizer')->wordsArray(strip_tags($page->summary_textarea))); | |
$count['article_word_count'] = count(wire('sanitizer')->wordsArray(strip_tags($page->rte))); | |
$total_words = array_sum($count); | |
// Set the value for the reading time field (number) based on word count | |
// To provide a range, a text value would have to be returned (and stored), but is possible by calculating cpm, | |
// wpm_top, wpm_bot and determining variance | |
// Source Study: https://iovs.arvojournals.org/article.aspx?articleid=2166061 | |
$cpm = 987; // average characters per minute, for English | |
$wpm_bot = 161; // English values for reading speeds | |
$wpm_top = 228; | |
$wpm_ave = ceil(($wpm_bot + $wpm_top) / 2); | |
$page->number_1 = ceil(($total_words) / $wpm_ave); | |
// Populate back arguments (assuming they've been modified) | |
$event->arguments(0, $page); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment