Share

How to Display Reading Time on a WordPress Blog Without Using a Plugin

Display Reading Time on a WordPress Blog Without a Plugin

In the digital era, content creators aim to optimize user experience and engagement on their websites. For bloggers, offering readers an estimated WordPress reading time without a plugin for articles enhances user interaction and helps manage expectations.

While several plugins are available for this purpose, there’s a growing preference to minimize plugin usage due to security concerns, site speed, or customization requirements. This article explores a method on how to display reading time on a WordPress blog without a plugin.

Why Displaying Reading Time on a WordPress Blog is Important?

Before we jump into the technical aspects, let’s briefly talk about the benefits of showing reading time. Displaying reading time on your WordPress blog has several advantages:

  • Improved User Experience: Readers appreciate knowing how much time they need to invest in an article. It allows them to manage their time more effectively.
  • Increased Engagement: When readers know the approximate reading time, they are more likely to stay and read the entire article, leading to higher engagement and reduced bounce rates.
  • Content Planning: Displaying reading time can help you plan your content better. You can create shorter or longer articles based on your target audience’s preferences.

Steps to Display Reading Time on a WordPress Blog Without a Plugin

Now, let’s get started on adding this feature to your WordPress blog.

Step 1: Access Your WordPress Theme Files

  1. Log in to your WordPress dashboard or admin panel.
  2. Navigate to Appearance and select Theme Editor.

Step 2: Modify Your Theme’s Functions.php

  1. In the Theme Editor, locate the functions.php file on the right sidebar. Click on it to open the file.
  2. Add the following PHP code to your functions.php file:
/**
 * Calculate Reading time for the content
 *
 * @param string $content
 * @return int
 */
function calculate_reading_time( $content ) {
	$word_count    = str_word_count( strip_tags( $content ) );
	$reading_speed = 200; // Adjust this value as per your audience.
	$reading_time  = ceil( $word_count / $reading_speed );
	return $reading_time;
}

/**
 * Display Reading time
 *
 * @return string
 */
function display_reading_time() {
	$content      = get_post_field( 'post_content', get_the_ID() );
	$reading_time = calculate_reading_time( $content );
	if ( 1 === $reading_time ) {
		return sprintf( __( 'Estimated reading time: %s minute', 'text_domain' ), $reading_time );
	} else {
		return sprintf( __( 'Estimated reading time: %s minutes', 'text_domain' ), $reading_time );
	}
}

// Adding shortcode to display the reading time in the blog
add_shortcode( 'reading_time', 'display_reading_time' );

This code defines two functions: calculate_reading_time and display_reading_time. The first function calculates the reading time based on a predefined number of words per minute, while the second function displays the reading time in minutes.

  1. Modify the $words_per_minute variable in the calculate_reading_time function to reflect your desired reading speed. The default value is 200, which assumes an average reading speed.
  2. Save the changes to your functions.php file.

Step 3: Display Reading Time in Your Posts

  1. Open the post editor for the post where you want to display the reading time.
  2. Add the following shortcode wherever you want to display the reading time:
[reading_time]

Also, you can call the function and place it anywhere using the below PHP code:

<?php echo display_reading_time(); ?>

Step 4: Save Changes and Test

  1. Save your post.
  2. Visit your blog and open the post where you added the shortcode. You should now see the estimated reading time displayed.
Display Reading Time on a WordPress Blog

Customization and Styling

  1. CSS Customization: Styling the reading time can enhance its visibility. Use CSS to modify the appearance according to your theme’s design.
  2. Customizing Text and Language: Adapt the text to match your blog’s language and style. You can adjust the messages for longer reading times to enhance audience engagement.

Testing and Refinement

  1. Testing the Functionality: Before deploying the changes, test the feature on different posts to ensure accurate reading time calculations.
  2. Refinement and Optimization: Continuously refine the code and seek optimization opportunities. Consider adjusting the reading speed parameter or implementing caching techniques to improve performance.

Conclusion

Offering readers the estimated reading time of an article without relying on additional plugins can positively impact user experience on a WordPress blog.

By employing simple code integration, customization, and a bit of testing, content creators can enhance their audience’s browsing experience, making content more accessible and engaging.

Implementing this WordPress reading time without a plugin underscores the ability to customize and optimize a WordPress blog while prioritizing speed and security.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
This website uses cookies to ensure you get the best experience on our website. By continuing to use this site, you agree to the use of cookies in accordance with our Cookie Policy.