Last active
July 19, 2019 22:57
-
-
Save mustafakucuk/d40c03ae637d60c86f4a53ca282a09d8 to your computer and use it in GitHub Desktop.
Adding Estimated Reading Time to WordPress.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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