Archived topic
reading time code to add to entry meta
7 replies · Started by Carlo on June 30, 2021
i have set my post to show author, published date and reading time under my blog title.
unfortunately, the reading time is via a plugin (with a shortcode), with their lates update the shortcode no longer works so my entry meta looks all messed up.
is it possible within generatepress (or via PHP code perhaps) to add some reading time code so I don't need to reply on the plugin shortcode to achieve this.
currently, I'm using the following code via the "code snippets" plugin. the bit i'd like to replace is the "[editorskit display="wordcount" before="Reading Time: " after=" min"]" short code.
add_filter( 'generate_post_author', '__return_false' );
add_filter( 'generate_show_comments', '__return_false' );
add_filter( 'generate_post_date_output', function( $date, $time_string ) {
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>';
}
printf( '<span class="posted-on">%s</span>',
$time_string
);
echo do_shortcode( '[editorskit display="wordcount" before="Reading Time: " after=" min"]' );
}, 10, 2 );
Hi there,
maybe this topic will help where Tom provides a Reading time function:
https://generatepress.com/forums/topic/post-visit-counter-and-mins-read-time/page/2/#post-1586449
ok i read through that, so as far as i can see, there is a mention of code there for reading time on this post: https://generatepress.com/forums/topic/see-the-reading-time-in-a-post-page-home/#post-1060767
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;
} );
so how do I incorporate that, so it replaces the reading time shortcode i was using in my original code?
First you only need to add this function from Toms 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 );
var_dump( $time );
return $time;
}
Then in your original code, change this line:
echo do_shortcode( '[editorskit display="wordcount" before="Reading Time: " after=" min"]' );
to:
echo '<div class="read-time">Reading time: ' . tu_estimated_reading_time() . '</div>';
didn't quite work unfortunately,
I added the function like you mentioned above,and changed my code so now my code looks like this:
add_filter( 'generate_post_author', '__return_false' );
add_filter( 'generate_show_comments', '__return_false' );
add_filter( 'generate_post_date_output', function( $date, $time_string ) {
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>';
}
printf( '<span class="posted-on">%s</span>',
$time_string
);
echo '<div class="read-time">Reading Time: ' . tu_estimated_reading_time() . '</div>';
}, 10, 2 );
I've also adjusted the styling CSS and replaced the old shortcode class and added the "read-time" class so CSS styles that.
so now I have this weird "float(7)" next to the published/updated date. Like this:
June 30, 2021float(7) Reading Time: 7
also i'd like it to say "min" after the reading time number, so it can look like this:
June 11, 2021 Reading Time: 5 min
Sorry my bad - change the Function i provided to this:
function tu_estimated_reading_time() {
$post = get_post();
$content = $post->post_content;
$wpm = 250; // 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 . ' min';
}
add_shortcode('reading-time', 'tu_estimated_reading_time');
Then update this:
echo '<div class="read-time">Reading Time: ' . tu_estimated_reading_time() . '</div>';
to:
echo '<div class="read-time">Reading Time: ' . do_shortcode( '[reading-time]' ) . '</div>';
there we go :)
works perfect. Thanks so much David
Glad to hear that!