Site logo

[Resolved] Estimated Reading Time in Minutes and Seconds

Home Forums Support [Resolved] Estimated Reading Time in Minutes and Seconds

Home Forums Support Estimated Reading Time in Minutes and Seconds

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #2158566
    Melvin

    Hi,

    I’ve followed the instructions here and added estimated reading time to my site successfully.

    However, most of my posts are between 45 seconds to 1 minute 20 seconds. As a result, the Estimated Reading Time is always 1 minutes.

    Firstly, 1 minutes (instead of the singular minute) is already grammatically incorrect. Secondly, if the result is always 1 minute, it’s as good as not showing.

    Is there a way to show the estimated reading time in minutes and seconds? Like 45 seconds, 1 minute 30 seconds, or 2 minutes 10 seconds?

    #2158571
    Fernando
    Customer Support

    Hi Melvin,

    I see. If that’s the case, can you try this code instead and see if it works for you?:

    function tu_estimated_reading_time() {
        $post = get_post();
        $content = $post->post_content;
        $wpm = 300; // How many words per minute.
    
        $clean_content = strip_shortcodes( $content );
        $clean_content = strip_tags( $clean_content );
        $word_count = str_word_count( $clean_content );
    	$m = floor($word_count / 200);
        $s = floor($word_count % $wpm / ($wpm / 60));
    	$time = $m . ' minute' . ($m == 1 ? '' : 's') . ', ' . $s . ' second' . ($s == 1 ? '' : 's');
        return $time;
    }
    
    add_filter( 'generate_post_date_output', function( $output ) {
        $output .= '<div class="read-time">Reading time: ' . tu_estimated_reading_time() . '</div>';
    
        return $output;
    } );

    Kindly let us know how it goes. 🙂

    #2158577
    Melvin

    Hey Fernando,

    Thanks for the quick response. It works!

    However, is there a way to change minutes to minute if it’s 0?

    Like “0 minute, 42 seconds” instead of “0 minutes, 42 seconds”?

    #2158585
    Melvin

    Hey Fernando,

    If it’s too difficult, don’t bother. I’ll just use “m” and “s” instead. Like 0m:42s. No need to stress with singular and plural. 🙂

    #2158591
    Fernando
    Customer Support

    I see.

    Can you try this instead?:

    function tu_estimated_reading_time() {
        $post = get_post();
        $content = $post->post_content;
        $wpm = 300; // How many words per minute.
    
        $clean_content = strip_shortcodes( $content );
        $clean_content = strip_tags( $clean_content );
        $word_count = str_word_count( $clean_content );
    	$m = floor($word_count / 200);
        $s = floor($word_count % $wpm / ($wpm / 60));
    	$time = $m . ' minute' . ($m <= 1 ? '' : 's') . ', ' . $s . ' second' . ($s <= 1 ? '' : 's');
        return $time;
    }
    
    add_filter( 'generate_post_date_output', function( $output ) {
        $output .= '<div class="read-time">Reading time: ' . tu_estimated_reading_time() . '</div>';
    
        return $output;
    } );

    Kindly let us know how it goes. 🙂

    #2158640
    Melvin

    Hey Fernando,

    Yes, it worked perfectly!

    Thank you so much for your help. I appreciate you.

    Best regards,
    Melvin Neo

    #2161437
    Fernando
    Customer Support

    You’re welcome Melvin! Glad that worked! Feel free to reach out anytime if you’ll need assistance with anything else. 🙂

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.