Reading time shortcode
Type [reading_time] in a post or template to show “4 min read”. [reading_time wpm="180"] changes the reading speed.
<?php
// Change the text to your language: %d is the number of minutes.
$format = '%d min read';
$words_per_minute = max( 50, (int) ( $atts['wpm'] ?? 220 ) );
$text = wp_strip_all_tags( strip_shortcodes( (string) get_post_field( 'post_content', get_the_ID() ) ) );
$words = count( preg_split( '/\s+/u', trim( $text ), -1, PREG_SPLIT_NO_EMPTY ) ?: array() );
echo esc_html( sprintf( $format, max( 1, (int) ceil( $words / $words_per_minute ) ) ) );
How to use it
Type [reading_time] in a post, a page or a Shortcode block.
In these packs
- Content and media: Shortcodes, links, images and small design touches for the public site.