Skip to content

Instantly share code, notes, and snippets.

@mustafakucuk
Last active July 19, 2019 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mustafakucuk/d40c03ae637d60c86f4a53ca282a09d8 to your computer and use it in GitHub Desktop.
Save mustafakucuk/d40c03ae637d60c86f4a53ca282a09d8 to your computer and use it in GitHub Desktop.
Adding Estimated Reading Time to WordPress.
<?php
// For Plugin: https://tr.wordpress.org/plugins/reading-time-wp/
function reading_time($post_id) {
// Words per minute
$wpm = 150;
$post_content = get_post_field('post_content', $post_id);
$number_of_images = substr_count(strtolower($post_content), '<img ');
$post_content = strip_shortcodes($post_content);
$post_content = wp_strip_all_tags($post_content);
$word_count = count(preg_split('/\s+/', $post_content));
$reading_time = ceil($word_count / $wpm);
// If the reading time is 0 then return it as < 1 instead of 0.
if ($reading_time < 1) {
$reading_time = __('less than one minute', 'text_domain');
} elseif ($reading_time == 1) {
$reading_time = sprintf(__('1 minute', 'text_domain'), $reading_time);
} else {
$reading_time = sprintf(__('%s minutes', 'text_domain'), $reading_time);
}
return $reading_time;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment