[Resolved] Reading time meta in page header

Home Forums Support Reading time meta in page header

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1390982
    Bogdan

    Hello,

    I’ve been using this snippet to calculate the reading time for every article on my site and display it as meta information next to the date and author name:

    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 . ' minutes';
    }
    
    add_filter( 'generate_post_date_output', function( $output ) {
        $output .= '<div class="read-time">Reading time: ' . tu_estimated_reading_time() . '</div>';
    
        return $output;
    } );

    It was working perfectly until I changed the design and added the meta to the page header using elements. My Header element now looks like this:

    <h1>{{post_title}}</h1>
    <br>
    <br>
    <br>
    by {{post_author}} | {{post_date}} | {{post_terms.category}}

    It’s a gorgeous look, but I’m missing the reading time.
    How can I add it inside the header element?

    #1391150
    David
    Staff
    Customer Support

    Hi there,

    the easiest fix is to also register the function as a shortcode like so:

    add_shortcode('reading-time', 'tu_estimated_reading_time');

    Then you can add it your header element using [reading-time]

    #1392063
    Bogdan

    Thanks David! That worked like a charm. It now looks exactly like I wanted

    #1392126
    David
    Staff
    Customer Support
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.