How to Add Reading time in WordPress without using plugins | Er. Sahil

Adding reading time to your WordPress posts without using plugins can be done by adding a custom function to your theme’s functions.php file. Here’s a step-by-step guide:

Step 1: Calculate Reading Time

Open your theme’s functions.php file:

  • Go to your WordPress dashboard.
  • Navigate to Appearance > Theme Editor.
  • Select the functions.php file from the list on the right.

Add the following function:

function estimated_reading_time($content) {  
$word_count = str_word_count(strip_tags($content));
$reading_time = ceil($word_count / 200); // Average reading speed is 200 words per minute
return $reading_time;
}

Step 2: Display Reading Time in Posts

Add the reading time display to your theme:

Still in the functions.php file, you can create a function to display the reading time.

function display_reading_time() {  
if (is_single()) {
global $post;
$reading_time = estimated_reading_time($post->post_content);
echo '<p>Estimated Reading Time: ' . $reading_time . ' minute' . ($reading_time > 1 ? 's' : '') . '</p>';
}
}

Hook this function into your theme:

  • Find the appropriate place in your theme files (like single.php or content.php) where you want the reading time to appear.
  • Add the following line of code where you want the reading time to be displayed:
<?php display_reading_time(); ?> 

Step 3: Save Changes

  • After adding the code, save your changes to the functions.php file.

Step 4: Test It

  • Visit a single post on your site to see if the estimated reading time displays correctly.

Additional Notes

  • Make sure to back up your functions.php file before making any changes.
  • If you are using a child theme, it’s better to add the code there to avoid losing changes when the theme is updated.

This method allows you to add reading time functionality without relying on plugins, keeping your site lightweight and efficient.

Share your love