Estimation of reading time of a post is a really helpful feature. In this article, we’ll show you a custom PHP function which you can add that to the source code of your WordPress theme
Ready to use PHP snippet
Here you can find the entire snippet 👇
/**
* Estimated reading time in minutes
*
* @param $content
* @param $words_per_minute
* @param $with_gutenberg
*
* @return int estimated time in minutes
*/
function estimate_reading_time_in_minutes ( $content = '', $words_per_minute = 300, $with_gutenberg = false ) {
// In case if content is build with gutenberg parse blocks
if ( $with_gutenberg ) {
$blocks = parse_blocks( $content );
$contentHtml = '';
foreach ( $blocks as $block ) {
$contentHtml .= render_block( $block );
}
$content = $contentHtml;
}
// Remove HTML tags from string
$content = wp_strip_all_tags( $content );
// When content is empty return 0
if ( !$content ) {
return 0;
}
// Count words containing string
$words_count = str_word_count( $content );
// Calculate time for read all words and round
$minutes = ceil( $words_count / $words_per_minute );
return $minutes;
}
Line by line description of the custom PHP function
In the steps below we’ll describe the above code line by line:
1. At the beginning we conditionally process Gutenberg blocks into HTML code we can work with if required. More info is in the Usage of the function section at the end of this article
if ( $with_gutenberg ) {
$blocks = parse_blocks( $content );
$contentHtml = '';
foreach ( $blocks as $block ) {
$contentHtml .= render_block( $block );
}
$content = $contentHtml;
}
2. Then we want to remove all of the HTML tags from the content
$content = wp_strip_all_tags( $content );
3. After clearing all of the unnecessary characters we want to make sure that content is not empty
if ( !$content ) {
return 0;
}
4. In the $words_count
variable we want to count the number of words
$words_count = str_word_count( $content );
5. Finally we want to calculate the time needed to read the content assuming by default that we’re able to read 300 words per minute
$minutes = ceil( $words_count / $words_per_minute );
Usage of the function
You can use the function with 3 parameters:
Method 1: Basic usage
Counting time needed to read WordPress Blog Post
estimate_reading_timein_minutes( get_the_content() );
Method 2: Changing number of words per minute
By default, we assume that human is able to read 300 words per minute, but you can adjust that to your needs, ie. for some articles with hard words
estimate_reading_timein_minutes( get_the_content(), 200 );
Method 3: Assuming that content is based on the Gutenberg blocks
In many cases, you wouldn’t need to parse Gutenberg blocks. For example, if you call get_the_content()
function in the page.php file WordPress will parse blocks for you, so you will get processed HTML code — in this case you should use $with_gutenberg
param as false
. However, if you use a custom function and you try to get post content by post ID, then you might want to set $with_gutenberg
to true
for process Gutenberg blocks into HTML:
estimate_reading_timein_minutes( get_the_content(), 200, true );
Summary
That’s it 🎉 If you have any thoughts about this snippet feel free to leave a comment or share this article in social media 😊
What about ACF? If a post has ACF, how this function could count time with these fields?
Thank you for the training
And how can I use it as a shortcode in Elementor?