Code Snippets

Estimated Reading Time Using PHP

Estimated reading: 3 minutes 100 views

Ever wondered how to get reading time on your posts? Typically, you see reading times on food blogs, check out our article Start a Food Blog from Scratch: Everything You Need to Know to Launch, Grow, & Monetize! and the estimated reading time in meta. The snippet can be added anywhere, but it’s more favorable to add it in or close to the meta at the top of your page.

If you like the layout of our post, then check out our Delicious theme, it uses the same structure. This theme is specifically a pro theme because it uses many Kadence Pro features such as the Element Hooks for the post setup.

Here’s the exact snippet we use on our websites, the instructions are below the snippet code.

Code Snippet

/**
 * 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' );

Where to Add This Code

Here are two easy options:

Option 1: Install WPCode Lite Plugin

This is by far the safest method and is beginner friendly if you’re not used to editing theme files.

  1. Search for WPCode Lite plugin in your website dashboard.
  2. Install and activate the plugin.
  3. Go to Snippets > Add New.
  4. Give your snippet a name like “Estimated Reading Time.”
  5. Paste the code above.
  6. Choose â€śRun snippet everywhere”.
  7. Choose PHP, not CSS or any other.
  8. Save and activate.

âś… Done! Gutenberg is now enabled for your WooCommerce product pages.

Option 2: Functions.php

We don’t advise this option unless you’re a developer or if you feel comfortable editing theme files:

  1. Open your functions.php file.
  2. Paste the snippet at the bottom.
  3. Save and upload if needed.

đź’ˇ Tip: Always use a child theme to avoid losing changes when your theme updates.

Adding The Shortcode

Pace the shortcode wherever you want your readers to see it.

[reading_time]

NOTE: We use WPCode Lite because it’s more flexible than others, there’s no need to upgrade to pro unless you’re a developer, it offers many free snippets, can choose where to run it, won’t publish if the code is wrong so won’t break your site, PHP is included compared to others you have to buy the their pro, and best of all the free version can easily be downloaded from your WordPress Repository.

Leave a Reply

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

Share this Doc

Estimated Reading Time Using PHP

Or copy link

CONTENTS

Join The Party For Fun Updates freebies & Big Discounts!

Add your name
Add your email

JoiN Glamsy Cloud! Free STUNNING Kadence Templates Included!

Start with 5 gorgeous Kadence blocks on us. Love them? Unlock ‘50 FREE FOREVER‘ that’s 50 more free blocks just for choosing any essential tool from our trusted fav stack!

Grab Your Free Templates

Terms Of Use FAQs

Payment FAQs

Order FAQs

Support FAQs

Popular Pre-Sales FAQs

Redesigning FAQs

Adding A Sticky Sidebar Using CSS

If you prefer using custom code to create a sticky sidebar, create or edit a pag

Scroll To Top Using Code Using CSS

First, add the button HTML somewhere on your webpage, ideally at the bottom of t

Choose Your Hosting

Like many people, we started our online journey with one of the big-name hosts—a

Start Here

Glamsy Designs is thrilled to introduce a new documentation system that makes ac

Frequently Asked Questions

Creating Sticky Sidebars Using CSS

We are covering three ways of creating sticky sidebars using Kadence, CSS code,

Chat Icon Close Icon
Review Your Cart
0
Add Coupon Code
Subtotal

 

Subscribe

×
Cancel