How to Add a Post Read Time Element to Posts in Elementor: No Plugin Needed

TL;DR: To display an estimated read time for your WordPress posts directly in Elementor without using extra plugins, you can utilize a custom PHP function and Elementor’s dynamic tags feature. By adding a snippet of code using the Code Snippets plugin, you can create a custom active tag in Elementor Pro that calculates and shows the read time for each post, enhancing user engagement and providing valuable insights to your readers.

When enhancing user experience on a website, providing readers with an estimated read time for your articles can be incredibly beneficial. 

It sets expectations for the time investment required and can encourage visitors to commit to reading.

If you’re using Elementor Pro, you don’t need an additional plugin to add this feature to your posts. With a simple custom function and the power of Elementor’s dynamic tags, you can easily display the estimated reading time on your WordPress site. 

Let’s walk through the steps to implement this functionality without cluttering your WordPress installation with more plugins.

The Post-Read Time Function Overview

The custom function estimate_reading_time() calculates the estimated time for a person to read a WordPress post. It does so by counting the words in the post and dividing it by a standard reading speed (words per minute). You can adjust this if you want.

The function can dynamically display the reading time on posts when using Elementor Pro by creating a custom dynamic tag.

// Include the necessary WordPress admin file for is_plugin_active() function
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

if ( is_plugin_active( 'elementor-pro/elementor-pro.php' ) ) {
    // Elementor Pro is active, so register your dynamic tag action
    add_action('elementor/dynamic_tags/register_tags', function( $dynamic_tags ) {
        // Include the Tag class file
        include_once 'path_to_your_reading_time_tag_class.php';

        // Finally register the tag
        $dynamic_tags->register_tag( 'Reading_Time_Tag' );
    });

    class Reading_Time_Tag extends \Elementor\Core\DynamicTags\Tag {

        public function get_name() {
            return 'reading-time';
        }

        public function get_title() {
            return __('Reading Time', 'text-domain');
        }

        public function get_group() {
            return 'post';
        }

        public function get_categories() {
            return [ \Elementor\Modules\DynamicTags\Module::TEXT_CATEGORY ];
        }

        protected function register_controls() {
            // Add any necessary controls for your dynamic tag here
        }

        public function render() {
            $post_id = get_the_ID();
            $content = get_post_field('post_content', $post_id);
            echo estimate_reading_time($content);
        }
    }
}

// Estimate_reading_time() function
function estimate_reading_time($content) {
    $word_count = str_word_count(strip_tags($content));
    $words_per_minute = 200; // Average reading speed (words per minute)
    $reading_time = ceil($word_count / $words_per_minute);
    return $reading_time . ' minute' . ($reading_time === 1 ? '' : 's') . ' read';
}

How to Use with Code Snippets Plugin

The Code Snippets plugin for WordPress allows you to add PHP code snippets to your site without directly editing your theme’s functions.php file. We highly recommend it. Here’s how to use the function within the Code Snippets plugin:

Install and Activate the Code Snippets Plugin:

In your WordPress dashboard, navigate to ‘Plugins’ > ‘Add New’. Search for ‘Code Snippets’, install and then activate the plugin.

Create a New Snippet:

Once the plugin is activated, go to ‘Snippets’ > ‘Add New’ in your WordPress dashboard to add a new snippet.

Add the Custom Function and Elementor Hook:

Copy the PHP code for the custom function and Elementor dynamic tag (included in the previous responses) into the code area of the new snippet.

Make sure that:

  • You include the check for Elementor Pro activation.
  • Snippet is set to run everywhere.

Activate the Snippet:

Once you’ve added the code, click ‘Save Changes and Activate’ to activate the snippet. The function will now be active on your site.

Use the Custom Tag in Elementor:

With Elementor Pro active, and the Code Snippets snippet activated, you can now use the custom dynamic tag within Elementor to display the estimated reading time.

Edit a page with Elementor, and where you want to display the reading time, use dynamic tags to select the custom tag named “Reading Time.” It will automatically display the estimated reading time for the post.

2023 11 18 06

Conclusion

By integrating a custom function with the Code Snippets plugin, you can enhance the functionality of your WordPress site and Elementor Pro without modifying core theme files. This approach streamlines the process and provides a more secure and update-proof method of adding custom PHP code to your website.

Always test your snippets on a staging environment before applying them to your live site to prevent potential issues.

Written By
Adam
Founder of WP Helper.
You will also like these articles

Relax Knowing Your WordPress Site Is Secure & Running Smoothly 24/7

Let us manage your WordPress site, everything from security to updates will be taken care of. Support plans also come with dedicated support so we can do anything from adding content to customising your site for you.

Same Day Professional WordPress Support

Get WordPress Support Today

Need help with a single WordPress problem, today? We can help with anything from adding analytics tracking code to site hack recovery. Full money back guarantee on all jobs.

1. Submit a Support Request

Use our support ticket form below to send details of your problem to our developers.

2. Get a Quote

We will review your request and provide a quote within 24 hours (but usually within a few hours).

3. We fix your WordPress problem

Our team will begin fixing your WordPress problem the same day.

4. 100% Money Back Guarantee

If we can’t fix the problem for the amount quoted we will refund you 100%.

  • Do you have a screenshot of the issue or have a copy of the theme or plugin that is at fault? If you want to upload php, html or css please zip first.
    Drop files here or
    Accepted file types: jpg, jpeg, png, pdf, zip, gzip, rar, doc, txt, Max. file size: 15 MB, Max. files: 10.