Last active
February 11, 2023 20:57
-
-
Save devinsays/cd6b089dd2f138bcf668d5f938e38dbf to your computer and use it in GitHub Desktop.
WordPress Estimated Read Time
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 | |
/** | |
* Estimates the reading time for a given piece of $content. | |
* | |
* @param string $content Content to calculate read time for. | |
* @param int $wpm Estimated words per minute of reader. | |
* | |
* @returns int $time Esimated reading time. | |
*/ | |
function prefix_estimated_reading_time( $content = '', $wpm = 300 ) { | |
$clean_content = strip_shortcodes( $content ); | |
$clean_content = strip_tags( $clean_content ); | |
$word_count = str_word_count( $clean_content ); | |
$time = ceil( $word_count / $wpm ); | |
return $time; | |
} |
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
<div class="reading-time"> | |
<?php echo prefix_estimated_reading_time( get_the_content() ); ?> min read | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment