Skip to content

Instantly share code, notes, and snippets.

@Apina
Last active August 29, 2015 13:58
Show Gist options
  • Save Apina/10346836 to your computer and use it in GitHub Desktop.
Save Apina/10346836 to your computer and use it in GitHub Desktop.
Estimated Read Time plugin for WordPress
<?php
/*
Plugin Name: Estimated Read Time
Plugin URI: http://www.apinapress.com/
Description: Adds an estimated read time to posts (just above post content)
Version: 0.0.1
Author: Dean Robinson
Author URI: http://www.apinapress.com
License: GPL3
*/
function ap_reading_time($content) {
if (!in_the_loop () || !is_main_query ()) {
return $content;
}
if(is_singular('post') ) {
$count = str_word_count(wp_strip_all_tags($content) );
$count = $count/200;
//let's stop those pesky 3 digit numbers
$count = round ( $count, 2 );
$x = explode('.', $count);
if($x[0] == 1) { $minutetext = "minute"; } else { $minutetext = "minutes"; }
$min = $x[0] . " " . $minutetext . " ";
if (isset($x[1])) {
$sec= 0.6 * $x[1];
if($sec == "1") { $secondtext= " second"; } else { $secondtext= " seconds"; }
$min = $min . round($sec) . $secondtext;
}
$content = "<p class='ap_reading_time'><span>Estimated reading time: </span>" . $min. "</p>" . $content;
return $content;
}
else {
return $content;
}
}
add_filter('the_content', 'ap_reading_time');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment