Introduction

The estimated reading span of the wordpress post gives how long time is required to read it for your reader. This can be given in minutes, second, or some other scale. It gives a rough idea about the time it will take to complete the reading of the post. It is based on a base value of a total number of words that can be readable in a minute.

Step1. Ready customizer to set wordpress post estimated reading time

Make a customizer option to set the number of words that can be read per minute. This is the base value to calculate such reading time. Suppose if it is set 100 then it means it assumes that viewers normally take 1 minute to read 100 words. Thus if there are 1000 words in your post then it assumes that the post will take 10 minutes to read it completely.

Code for setting customizer interface for reading time.

$wp_customize->add_setting('global_show_min_read_number',
 array(              
   'default' => 100,              
   'capability' => 'edit_theme_options',              
   'sanitize_callback' => 'absint',          
 )      
);      
$wp_customize->add_control('global_show_min_read_number',          
 array(              
  'label' => esc_html__('Number of Words per Minute Read', 'isha'),              
  'section' => 'your_section_name',              
  'type' => 'number',              
  'priority' => 130          
 )      
);

Step2. Make wordpress post reading time estimated function

Make a function and add theme in function.php or any other files. Below is the function to calculate such reading time based on the value set in customizer. This function first calculate how many total words are present in the wordpress post and then calculates the reading time.

Code for setting estimated function.

/* Word read count */
if (!function_exists('your_function_name_reading_time')) :
function your_function_name_reading_time($post_id) {
  $content = apply_filters('the_content', get_post_field('post_content', $post_id));            
  $read_words = esc_attr(get_theme_mod('global_show_min_read_number','10'));            
  $decode_content = html_entity_decode($content);            
  $filter_shortcode = do_shortcode($decode_content);            
  $strip_tags = wp_strip_all_tags($filter_shortcode, true);            
  $count = str_word_count($strip_tags);            
  $word_per_min = (absint($count) / $read_words);            
  $word_per_min = ceil($word_per_min);
  if ( absint($word_per_min) > 0) {
    $word_count_strings = sprintf(_n('%s min  ', '%s min  ', 
    number_format_i18n($word_per_min), 'isha'), number_format_i18n($word_per_min));                
    if ('post' == get_post_type($post_id)):                    
      echo '<span class="min-read">';                    
      echo esc_html($word_count_strings);                    
      echo '</span>';                
    endif;            
  }            
  if ( absint($word_per_min) == Null) {                
    echo '<span class="min-read">';                
    echo '0 min';                
    echo '</span>';            
  }            
}
endif;

Step3. Show the post reading time count

After calculation then shows it in a required place of your wordpress site. You can chose the icon from here fontawesome. for example “fa fa fa-book”.

Code for display reading time at the page.

<li><i class="fa fa fa-book"></i><?php your_function_name_reading_time( get_the_ID() ); ?></li>

Below is the example from New Blog wordpress theme.

estimation reading time

This feature is available in many of our Free wordpress themes. Hence this code is truly and efficiently works.

Conclusion:-

If you are able to implement above code in your theme, you will definitely calculate the estimated reading time of wordpress post and display it without using any plugin. If need support then you can contact us. We will here to support you. Meanwhile you can see some of our cool free themes here.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.