Estimated Reading Time Above Posts
Shows a short "5 min read" line at the top of every blog post, calculated from the number of words in the post.
Opens WPIDE on
Code
// Average reading speed in words per minute.
$wpide_words_per_minute = 220;
// Text after the number of minutes.
$wpide_reading_label = 'min read';
add_filter( 'the_content', function ( $content ) use ( $wpide_words_per_minute, $wpide_reading_label ) {
if ( ! is_singular( 'post' ) || ! in_the_loop() || ! is_main_query() ) {
return $content;
}
$text = wp_strip_all_tags( strip_shortcodes( get_post_field( 'post_content', get_the_ID() ) ) );
$words = count( preg_split( '/\s+/u', trim( $text ), -1, PREG_SPLIT_NO_EMPTY ) );
$minutes = max( 1, (int) ceil( $words / max( 1, $wpide_words_per_minute ) ) );
return '<p class="wpide-reading-time" style="opacity:.75;font-size:.9em;">' . esc_html( $minutes . ' ' . $wpide_reading_label ) . '</p>' . $content;
}, 20 );
Notes
Adjust the reading speed in words per minute or the label text at the top of the snippet.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.