How to Display Estimated Reading Time for WordPress Posts Without Plugins

Understanding the estimated reading time of an article is crucial for enhancing user experience on your website. Estimated reading time provides visitors with a quick glance at how long it will take to read your content, thereby enhancing user engagement and satisfaction. By displaying this information, visitors can quickly assess the time commitment required to read your content.

In this tutorial, we will guide you through the process of calculating and displaying estimated reading time for WordPress posts without relying on plugins.

Before proceeding with any customizations in WordPress, it’s essential to set up a child theme. A child theme acts as a safe and efficient way to make modifications without affecting the parent theme. If you haven’t set up a child theme yet, follow this tutorial on How to Create a Child Theme for Customization. It will guide you through the process and ensure that your customizations remain intact even after theme updates.

Edit Functions.php File and Add Code Snippet

To start, you’ll need to access your WordPress child theme’s functions.php file. This file serves as the central hub for customizing your child theme’s functionality, ensuring that your modifications won’t be overridden during theme updates. You can achieve this by using a code editor or the built-in Theme Editor within your WordPress dashboard.

Once you have accessed the functions.php file, add the following code snippet at the end to implement the estimated reading time feature.

function custom_estimated_reading_time() {
	global $post;
	$words_per_minute = 250;
	$word_count       = str_word_count( strip_tags( get_post_field( 'post_content', $post->ID ) ) );
	$reading_time     = ceil( $word_count / $words_per_minute );

	$label = ' minute';
	if ( $reading_time > 1 ) {
		$label .= 's';
	}

	return 'Estimated Reading Time: ' . $reading_time . $label;
}

function register_custom_reading_time_shortcode() {
	add_shortcode( 'custom_reading_time', 'custom_estimated_reading_time' );
}

add_action( 'init', 'register_custom_reading_time_shortcode' );
Code language: PHP (php)

Explanation:

The custom_estimated_reading_time() function calculates the reading time based on the post’s word count and returns it with the appropriate label. Here’s a breakdown of the code:

  • global $post;: Accesses the global post variable to get the content of the current post.
  • $words_per_minute = 250;: Sets the average words per minute for reading.
  • $word_count = str_word_count(strip_tags(get_post_field('post_content', $post->ID)));: Counts the words in the post, stripping HTML tags for accurate calculation.
  • $reading_time = ceil($word_count / $words_per_minute);: Calculates reading time by dividing word count by words per minute and rounding up to the nearest whole number.
  • $label = " minute";: Initializes the label as singular.
  • if ($reading_time > 1) { $label .= "s"; }: Checks if the reading time is plural (more than 1 minute) and appends “s” to the label.
  • return "Estimated Reading Time: " . $reading_time . $label;: Returns the estimated reading time with the appropriate label (singular or plural) for visual appeal.

Using the Shortcode in Posts

You can now use the [custom_reading_time] shortcode within your posts and pages. Insert the shortcode where you want the estimated reading time to appear. To use the shortcode, simply add it to your post content, like this:

Here's a great article about WordPress!
[custom_reading_time]
Code language: PHP (php)

Integrating the Function Directly into single.php

Alternatively, you can integrate the custom_estimated_reading_time() function directly into your single.php file within your WordPress child theme. By doing this in the child theme, you ensure that your modifications are safe from theme updates. Here’s how you can do it:

Edit your child theme’s single.php file: Locate your single.php file inside your WordPress child theme folder (usually found in wp-content/themes/your-child-theme-name/single.php). If the single.php file is not present in your child theme, you can copy it from your parent theme and place it in your child theme folder. Edit the single.php file using a text editor or a code editor.

Integrate the function directly: Inside your single.php file, now paste the custom_estimated_reading_time() function directly into the file. To show the estimated reading time, typically before or after the post content, you can use the following:

// Define the custom_estimated_reading_time() function to calculate reading time.
function custom_estimated_reading_time() {
	global $post;
	$words_per_minute = 250;
	$word_count       = str_word_count( strip_tags( get_post_field( 'post_content', $post->ID ) ) );
	$reading_time     = ceil( $word_count / $words_per_minute );

	$label = ' minute';
	if ( $reading_time > 1 ) {
		$label .= 's';
	}

	return 'Estimated Reading Time: ' . $reading_time . $label;
}
?>

<div class="estimated-reading-time">
	<?php
	// Display the estimated reading time.
	echo esc_html( custom_estimated_reading_time() );
	?>
</div>
Code language: PHP (php)

Now, whenever you view a single post on your WordPress site, the estimated reading time will be displayed in the location where you added the custom_estimated_reading_time() function within your child theme’s single.php file. Remember, if your child theme doesn’t have a single.php file, you can copy it from the parent theme and customize it in your child theme to maintain your modifications even after theme updates.

That’s it! You’ve successfully implemented estimated reading time for your WordPress posts without using any plugins. By providing this valuable information to your readers, you enhance their experience and keep them engaged with your content. Remember, if your child theme doesn’t have a single.php file, you can copy it from the parent theme and customize it in your child theme to maintain your modifications even after theme updates.

Leave your feedback and help us improve 🐶

We hope you found this article helpful! If you have any questions, feedback, or spot any errors, please let us know in the comments. Your input is valuable and helps us improve. If you liked this article, please consider sharing it with others. And if you really enjoyed it, you can show your support by buying us a cup of coffee ☕️ or donating via PayPal 💰.

More free knowledge, because why not?

Your thoughts matter, leave a reply 💬

Your email address will not be published. Required fields are marked *