Skip to content

Instantly share code, notes, and snippets.

@wisnust
Last active January 4, 2023 21:06
Show Gist options
  • Save wisnust/227987457083605a34196b70848770ef to your computer and use it in GitHub Desktop.
Save wisnust/227987457083605a34196b70848770ef to your computer and use it in GitHub Desktop.
Estimated Reading time Wordpress without Plugin
<?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