How to add estimated reading time to WordPress posts and pages

 

How to add estimated reading time to WordPress posts and pages
Reading time

Does reporting reading time increase retention?

In some cases, adding this information can increase the amount of time visitors spend on your site.

From different analyses and experiences, at least that is said It is clear that people are more likely to visit an article when they know in advance the approximate time it will take them to read it.

How fast do we read?

Reading different texts some data or approximations are repeated, such as that the average reading speed of an adult is 240 words per minute with a comprehension rate of 60%, however, the reading speed decreases by about 25% when reading on a computer screen instead of on paper.

This is why in the example below the speed is set at 200 words per minute.

Add estimated reading time without plugin

This information can be added with a plugin or a bit of code. As it is dogma here to avoid installing a new plugin that serves only one simple thing (besides, most they are rather neglected) we explain how to do it with a snippet.

If you prefer something more visual and simple like a reading progress bar at the top that fills up as you read, here you can find how to add it without a plugin.

I have chosen and tested this code because it also takes into account the reading time of the images, which is set to 10 seconds.

Although this parameter is quite relative because it will depend on what kind of images are involved, their size and amount of elements and text they contain, etc. I think it balances the total count a bit more.

Not many blogs add the calculation of the reading time of the images, but as in this blog there are a lot of them and most of the vignettes, by their nature, don't have much text. So I've decided to leave it at 10 seconds and it still seems like a lot.

Medium for example, it measures at 265 words per minute with a setting made for the images of 12 seconds for the first, 11 for the second and 1 second less for each image after that. Any after the tenth image is counted in three seconds Source.

For Chinese, Japanese and Korean posts, Medium sets the number of characters to 500 per minute with the same setting as above for images

Code

This code could always be improved by adding the calculation of seconds or more complex variables depending on the height of images or other parameters to try to refine the data a little more. (I encourage anyone in the know to do so).

/*Reading time */

function wp_reading_time() {

// GET THE CONTENT OF THE WORDPRESS POST
$content = get_post_field( 'post_content', $post->ID );

// COUNT THE NUMBER OF WORDS
$word_count = str_word_count( strip_tags( $content ) );

// COUNT THE NUMBER OF IMAGES
$image_count = substr_count( $content, '<img' );

// READING TIME OF TEXTS โ€“ 200 WORDS PER MINUTE
$reading_time = $word_count / 200;

// READING TIME OF IMAGES โ€“ 10 SECONDS PER IMAGE
$image_time = ( $image_count * 10 ) / 60;

// ADD READING TIME OF TEXTS AND IMAGES
$total_time = round( $reading_time + $image_time );

// DETERMINE IF SINGULAR OR PLURAL
if ( $total_time == 1 ) { $minute = " minuto"; }
else { $minute = " minutos"; }

return $total_time . $minute;

}

There are several ways to add this code: downloading and editing functions.php locally, directly from the WordPress template editor (Appearance/Theme Editor) or editing it, also "on the fly", from your Cpanel file manager or similar.

If you don't feel brave enough to add this snippet to your template's functions.php file by hand, you can choose to do so using the plugin Code Snippets This way you can quickly and easily revert changes if something breaks or the code is no longer compatible with a PHP, plugin or WordPress core update

Then just add this code that will paint the minutes on your blog, where it reads "estimated reading time" you can write what you think is best.

<?php echo "Reading time: " . wp_reading_time(); ?>

You can add it wherever you like, although if you decide to put it in the post meta, where your template shows the author of the post, the date of publication, number of comments, etc. you must add the code to the post meta loop of the content.php or single.php file (depending on each template) or page.php if you want to add it to the pages as well.

After checking that it works, just adapt the formatting, add an icon or whatever. Here it looks like this:

How to add estimated reading time to WordPress posts and pages 1

I've chosen to add it with a shortcode created with Ad Inserter in the child theme in the template created with Elementor PRO for all posts

How to add estimated reading time to WordPress posts and pages
How to add estimated reading time to WordPress posts and pages

Although it sounds messy, it's a very clean option for not having to touch a single line of code in WordPress and being able to modify it quickly and without complications at any time.

Extra, word counter without plugin

A simple and quick way to add the number of words in each post. You can put it under the title or in the meta, or wherever you want and format it with some CSS.

1-Create shortcode with Code Snippet.

add_shortcode('contador', 'word_count');

2-The function is assigned.

function word_count() {
$content = get_post_field( 'post_content', get_the_ID());
$word_count = str_word_count( strip_tags( $content ) );
return $word_count;
}
echo ' ' , word_count();

Finally, by the same method described above, the shortcode "counter" is added with Elementor in the template.

And this is the result:

Contador de palabras sin plugin para WordPress

Suscrรญbete por email para recibir las viรฑetas y los artรญculos completos y sin publicidad

Subscription by e-mail

Receive free full articles in your inbox without advertising as soon as they are published. The full content of the feed is sent ad-free via an external service.