Site logo

Archived topic

Creating an Estimated Reading Time

33 replies · Started by Zad on August 9, 2018

Viewing posts 16–30 of 34

Nothing shows up at all?

Can you add this:

var_dump( $time );

After this:

$time = ceil( $word_count / $wpm );

Just to debug :)

Just added it!

Did it output any data on the front end of your website? I'm not seeing anything on your site, but it should at least output an empty variable. Is the code still there?

This is the code I have inserted and activated on the site. As far as I know, I don't see anything on the front end of the site,

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 );
    var_dump( $time );

    return $time;
}

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

    return $output;
} );

Are you using another function to modify the author as well? I assume you want this to show under the author/date?

Yes, using the code for the gravatar entry meta

add_filter( 'generate_post_author', '__return_false' );
add_filter( 'generate_show_comments', '__return_false' );
add_filter( 'generate_post_date_output', 'tu_fancy_byline' );
function tu_fancy_byline( $date ) {
	printf( ' <span class="byline">%1$s</span>',
		sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%4$s<a href="%1$s" title="%2$s" rel="author"><span class="author-name" itemprop="name">%3$s</span></a></span>',
			esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
			esc_attr( sprintf( __( 'View all posts by %s', 'generatepress' ), get_the_author() ) ),
			esc_html( get_the_author() ),
			get_avatar( get_the_author_meta( 'ID' ) )
		)
	);

	if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
		echo '<span class="comments-link">';
			comments_popup_link( __( 'Leave a comment', 'generatepress' ), __( '1 Comment', 'generatepress' ), __( '% Comments', 'generatepress' ) );
		echo '</span>';
	}

	echo $date;
}

And yes, it would hopefully be below that

In that case, we'd need to change:

generate_post_author_output

To:

generate_post_date_output

In your second function.

That way the reading time should be appended to the updated date, and then placed on its own line with the <div> element.

It works!! Any way to put minutes next to the output? And do I need to remove the debugging code?

This would be your final code:

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 . ' minutes';
}

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

    return $output;
} );

It worked!!!!!!!!! Thanks so much!!!

No problem :)

Just one last thing, I was wondering if it was possible to add a few spaces to the read time for the archives page because it doesn't look centered?

https://imgur.com/a/vVYb1D2

That or getting rid of the gravatar image from the gravatar entry meta function, as I don't really need the gravatar image, but simply wanted the name to be to the left of the date.

Never mind, edited the CSS to get it to work!

I already have an Estimated Reading Time plug-in. Can I show Estimated Reading Time using Elements after title? I played around a little but no luck so far.

This archived topic is closed to new replies.