Mecha CMS

Mecha CMS blog and documentation.

“Estimated Reading Time” Widget

Updated: Sunday, 07 August 2016

Gives the information needed to make a quick decision on whether to commit to reading an article or not.

Table of Content

/**
 * Estimate Time Required to Read the Article
 * ------------------------------------------
 *
 * => `http://www.binarymoon.co.uk/2013/10/wordpress-estimated-reading-time`
 *
 */

Widget::add('ERT', function($text = false) use($config, $speak) {
    if( ! $text) {
        $s = Config::get($config->page_type);
        $text = isset($s->content) ? $s->content : "";
    }
    $word = str_word_count(strip_tags($text));
    $minute = floor($word / 120);
    $second = floor($word % 120 / (120 / 60));
    $minute_text = $minute == 1 ? $speak->minute : $speak->minutes;
    $second_text = $second == 1 ? $speak->second : $speak->seconds;
    if($minute >= 1) {
        $time = $minute . ' ' . $minute_text . ', ' . $second . ' ' . $second_text;
    } else {
        $time = $second . ' ' . $second_text;
    }
    return $time;
});

Usage

Article and Page

<?php echo Widget::ERT(); ?>

Custom

<?php echo Widget::ERT('Lorem ipsum dolor sit amet.'); ?>
Donation and Email Subscription