Site logo

Archived topic

Creating an Estimated Reading Time

33 replies · Started by Zad on August 9, 2018

Viewing posts 1–15 of 34

Those should both work just fine with GeneratePress. Where do you want it to show up?

So first you'd add this function to your site:

function tu_estimated_reading_time() {

	$post = get_post();

	$words = str_word_count( strip_tags( $post->post_content ) );
	$minutes = floor( $words / 120 );
	$seconds = floor( $words % 120 / ( 120 / 60 ) );

	if ( $minutes < 2 ) {
		$estimated_time = $minutes . ' minute' . ($minutes == 1 ? '' : 's') . ', ' . $seconds . ' second' . ($seconds == 1 ? '' : 's');
	} else {
		$estimated_time = $seconds . ' second' . ($seconds == 1 ? '' : 's');
	}

	return $estimated_time;

}

Then you'd do this:

add_filter( 'generate_post_date_output', function( $output ) {
    return $output . ' ' . tu_estimated_reading_time();
} );

Let me know if that works or not :)

Thanks! Where would I add these codes? To the function.php file? Code snippets? Or hooks?

Hi, when I try to insert the code I get an error for this line

if ( 1 < = $minutes ) {

Apparently the = sign is unexpected

I just made an adjustment to the code above - can you check again?

Seems to put it in a weird place and also estimate the time in a weird way

I generally want the time below the author and date. My date is also the last updated date rather than the original post date, added CSS from the Documentation page for that

https://imgur.com/a/7Vc4q3J

You can try this for your second function instead:

add_filter( 'generate_post_author_output', function( $output ) {
    $output .= '<div class="read-time">Reading time: ' . tu_estimated_reading_time() . '</div>';

    return $output;
} );

It looks good now but for some reason the function isn't calculating the times correctly

https://imgur.com/a/gNXuXUr

Its estimating it to be abnormally low

That's just one of the functions you found.

We could try simplifying it..

function tu_estimated_reading_time() {
    $post = get_post();
    $content = $post->post_content;
    $wpm = 300; // How many words per minute.

    $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;
}

Doesn't seem to work. Maybe the function itself is fine but I think the add filter may need to be updated?

I have a gravatar entry meta and css code for last Updated.

https://imgur.com/a/2gpEccK

Did you keep this filter added?: https://generatepress.com/forums/topic/creating-an-estimated-reading-time/#post-645787

Your full functions should look like this:

function tu_estimated_reading_time() {
    $post = get_post();
    $content = $post->post_content;
    $wpm = 300; // How many words per minute.

    $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;
}

add_filter( 'generate_post_author_output', function( $output ) {
    $output .= '<div class="read-time">Reading time: ' . tu_estimated_reading_time() . '</div>';

    return $output;
} );

Yup, doesn't seem to work

This archived topic is closed to new replies.