Last active
January 4, 2023 21:06
-
-
Save wisnust/227987457083605a34196b70848770ef to your computer and use it in GitHub Desktop.
Estimated Reading time Wordpress without Plugin
This file contains hidden or 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 | |
function reading_time() { | |
$content = get_post_field( 'post_content', $post->ID ); | |
$word_count = str_word_count( strip_tags( $content ) ); | |
$reading_time_minutes = ceil($word_count / 200); | |
if ($reading_time_minutes == 1) { | |
$reading_time_label = 'minute'; | |
} else { | |
$reading_time_label = 'minutes'; | |
} | |
return '<p>Reading time: <span class="reading-time">' . $reading_time_minutes . '</span> ' . $reading_time_label . '</p>'; | |
} | |
// call this function to display on your content, example: content.php - echo reading_time(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment