2 min read

Table of Contents
AI-Friendly Page Summary

Learn how to add an estimated reading time to your WordPress posts without using a plugin. This step-by-step guide is written for designers and beginners, with a simple code snippet you can copy and use in your theme or post content.

Ever wondered how long it takes someone to read your blog post? Adding a simple “X min read” message is a great way to set expectations for your readers and keep them engaged. The best part is you don’t need a plugin or any advanced coding knowledge to add it to your WordPress site.

This guide will show you how to set up a reading time function and choose where you want it to appear, either in your layout or directly within your post content.

What You’ll Be Doing

  • Adding a small piece of code to your site
  • Choosing where the reading time should appear (template or content)
  • No plugin required, just one simple function

Step 1: Add the Reading Time Function

This is the core of it all, a short PHP function that counts the words in your post and estimates how many minutes it would take someone to read it.

To do this:

  1. Log in to your WordPress dashboard.
  2. Either:
    • Go to Appearance → Theme File Editor, and scroll to the bottom of your functions.php file
    • Or, use a plugin like Code Snippets (recommended for beginners) and create a new snippet
function get_read_time(string $post_content = '', int $wpm = 200): string {
    if (empty($post_content)) {
        $post_content = get_post_field('post_content', get_the_ID());
    }

    $word_count = str_word_count(strip_tags($post_content));
    $minutes = ceil($word_count / $wpm);

    return $minutes . ' min read';
}

3. Save or update the file or snippet

The function is now available. Next, decide how you want to display the reading time.This approach works best on a child theme or a theme you’ve built yourself, so future updates don’t overwrite your changes. If you’re using Code Snippets, your changes are safe regardless of theme updates.

Step 2: Choose How to Display the Reading Time

You’ll need to choose one of the following two options.

Option A: Add It to the Theme Automatically

If you want the reading time to appear in the same place on every blog post, you can add it to your theme template files.

To do this:

  1. Open your theme’s post template file (such as single.php or a block template if you’re using a Full Site Editing theme).
  2. Paste this line of code wherever you’d like the reading time to appear:

echo get_read_time();

You could place it above the post title or before the main content. This method will apply it consistently across your site.

Option B: Use a Shortcode Inside the Post Content

If you prefer to control where it appears in each post individually, you can create a shortcode.

To do this, add the following code to your functions.php file, just below the main function:

add_shortcode('read_time', function () {<br>return get_reading_time();<br>});

Now, when editing a post in WordPress, you can type this directly into the content:

[read_time]

This will show something like:

3 min read

Optional Adjustments

If your audience tends to read quickly or slowly, you can change the 200 to a different number to suit your needs
If you’d like to style the output, you can wrap it in a div or span with a CSS class and style it in your theme
This approach works with any theme and doesn’t require any additional plugins

Author: Matthew Reilly

Leave a Reply

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

Related

Xd Figma Penpot

Penpot: A Real Alternative to XD and Figma?

UI/UX
Scaling featured images

Scaling featured images between posts in Elementor

Wordpress Code
Menu Backup and Restore Wordpress Plugin

How to Back Up & Restore WordPress Menus with a Simple Plugin

Wordpress Plugin