[Resolved] Creating an Estimated Reading Time

Home Forums Support [Resolved] Creating an Estimated Reading Time

Home Forums Support Creating an Estimated Reading Time

Viewing 15 posts - 16 through 30 (of 34 total)
  • Author
    Posts
  • #646411
    Tom
    Lead Developer
    Lead Developer

    Nothing shows up at all?

    Can you add this:

    var_dump( $time );

    After this:

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

    Just to debug ๐Ÿ™‚

    #646582
    Zad

    Just added it!

    #646631
    Tom
    Lead Developer
    Lead Developer

    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?

    #646634
    Zad

    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;
    } );
    #646642
    Tom
    Lead Developer
    Lead Developer

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

    #646643
    Zad

    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

    #646644
    Tom
    Lead Developer
    Lead Developer

    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.

    #646646
    Zad

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

    #646647
    Zad

    woops, just noticed, it says “float3” next to the date

    View post on imgur.com

    #647138
    Tom
    Lead Developer
    Lead Developer

    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;
    } );
    #647165
    Zad

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

    #647412
    Tom
    Lead Developer
    Lead Developer

    No problem ๐Ÿ™‚

    #647415
    Zad

    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?

    View post on imgur.com

    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.

    #647421
    Zad

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

    #719986
    abacus

    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.

Viewing 15 posts - 16 through 30 (of 34 total)
  • The topic ‘Creating an Estimated Reading Time’ is closed to new replies.