Simple Estimated reading time PHP code WordPress script

📂 Category: PHP

🖺 Last modified: 2nd Jan 2020

💻 Experience: Beginner - Intermediate

🕑 Read Time: 4 min

Many blogs and web magazines display their articles estimated reading time. This information is usually displayed in the header of the article together with other meta data. Showing the read time estimate encourages visitors to read the post, as they know how much time it will take them. The result is increased visitors engagement on the web site and lower bounce rate. Additionally, user experience is also enhanced by displaying this information. There are plugins available for this purpose, but many developers will want to add a custom Estimated reading time PHP code WordPress script instead.

How does estimated reading time PHP code WordPress script work?

Depending on the readability of the text, average reading speed for adults is 200 to 300 words per minute. An article in which short sentences prevail is easier and faster to read. Accordingly, an article where long sentences prevail is harder and slower to read.

Estimated reading time PHP code WordPress script calculates the read time by simple math. Firstly, the script counts words in post content. Then it divides words quantity by predefined average reading speed per minute. Furthermore, it rounds the result to the closest whole number and displays it in the post template.

Estimated reading time PHP code for WordPress

Web magazine or blog visitors will more likely click on an article and read it, if they know how much time it will take them.

A simple Estimated reading time PHP script for WordPress

The following Estimated reading time PHP script calculates read time as described above. Furthermore it adds a customizable prefix and sufix to the numerical result.

	
/*  Reading time  */

function display_read_time() {
    $content = get_post_field( 'post_content', $post->ID );
    $count_words = str_word_count( strip_tags( $content ) );
	
    $read_time = ceil($count_words / 250);
	
	$prefix = "Read time: ";
    $suffix = " min.";
	
    $read_time_output = $prefix . $read_time . $suffix;

    return $read_time_output;
}

To add this script to your WordPress web site follow this easy steps:

  • enter the WordPress theme editor
  • preferable edit your child theme to avoid loosing your changes when updating the main theme
  • locate and open the Theme functions file functions.php
  • to define the reading time function, paste the Estimated reading time PHP script on the bottom of functions.php file
  • customize the average reading speed (set to 250 words per minute in this example) if needed
  • customize the prefix and suffix text if needed
  • save the changes

Pro Tip: Setting lower average read speed values (closer to 200) will result with longer read time estimates. Accordingly, setting higher average read speed values (closer to 300) will result with shorter read times. Shorter read time will more likely encourage visitors to read an article. On the other hand, if you write technical or other articles harder to understand, it’s better to estimate longer but realistic read times.

Displaying the Estimated reading time in article header

Defining the function that calculates the reading time is just the first part of the process. The second part is displaying the calculated result within the single post template, usually in the post header. To do that, it’s necessary to call the function, right after the post title.

The PHP code that calls the Estimated reading time function from our example and shows the calculated result is </?php echo display_read_time(); ?>. To display the result as a block element and to style it with CSS, additionally we’ll wrap the code with <div> and <p> HTML tags like this:

<div class="reading-time">
<p><?php echo display_read_time(); ?></p>
</div>

To embed it into your single post template, follow this steps:

  • enter the WordPress theme editor, preferable within your child theme
  • locate and open the Single post template single.php
  • find the position inside the post template right after the post title and before the post content
  • paste the code snippet above that calls the Estimated reading time function
  • save the changes

Pro Tip: There are slight differences in the way single.php post template is coded from theme to theme. In some WordPress themes, like for example Twenty Nineteen you won’t find the post title and post content blocks inside the single.php file. In the Twenty Nineteen theme, the code that renders the article title and content is located in the file content-single.php located inside the theme folder /template-parts/content/. Intermediate experienced developers shouldn’t have any problems locating this position inside any theme.

More tutorials on adding Estimated reading time to WordPress

If you’re not comfortable with code editing, read our review of the most used Free Estimated read time plugins. Otherwise, check our advanced tutorial and write a improved and more customizable Estimated reading time PHP script.