Site logo

Archived topic

Problem with snippets

7 replies · Started by Liran on April 25, 2021

Viewing posts 1–8 of 8

Hey,

I use this snippet to display the last updated date and it works great:

add_filter( 'generate_post_date_output', function( $output, $time_string ) {
    $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';

    if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
        $time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">Last Updated On %4$s</time>';
    }

    $time_string = sprintf( $time_string,
        esc_attr( get_the_date( 'c' ) ),
        esc_html( get_the_date() ),
        esc_attr( get_the_modified_date( 'c' ) ),
        esc_html( get_the_modified_date() )
    );

    return sprintf( '<span class="posted-on">%s</span> ',
        $time_string
    );
}, 10, 2 );

But when I activate this function to show the estimated reading time, I get nothing:

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 read';

}

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

    return $output;
} );

Any idea what's wrong?

Thanks!

Hi there,

Are you using both? Your 2 filters are in conflict.

Can you explain a bit more about what you're trying to achieve?

I would like to use both filters if possible.

I want to display the entry date published/updated, followed by the estimated reading time.

We can merge them into one filter. :)

Example:

add_filter( 'generate_post_date_output', function( $output, $time_string ) {
    $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';

    if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
        $time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">Last Updated On %4$s</time>';
    }

    $time_string = sprintf( $time_string,
        esc_attr( get_the_date( 'c' ) ),
        esc_html( get_the_date() ),
        esc_attr( get_the_modified_date( 'c' ) ),
        esc_html( get_the_modified_date() )
    );

    $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 sprintf( '<span class="posted-on">%1$s %2$s min read</span> ',
        $time_string,
        $time
    );
}, 10, 2 );

It works! Now, how do I add a vertical bar between them? :)

It works! Now, how do I add a vertical bar between them? 🙂

We may have to edit the filter to make it possible to add on in CSS. Do you have any specific vertical bar design you want to use?

Example:

add_filter( 'generate_post_date_output', function( $output, $time_string ) {
    $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';

    if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
        $time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">Last Updated On %4$s</time>';
    }

    $time_string = sprintf( $time_string,
        esc_attr( get_the_date( 'c' ) ),
        esc_html( get_the_date() ),
        esc_attr( get_the_modified_date( 'c' ) ),
        esc_html( get_the_modified_date() )
    );

    $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 sprintf( '<span class="posted-on">%1$s <span class="reading-time">%2$s min read</span></span> ',
        $time_string,
        $time
    );
}, 10, 2 );

After modifying the filter to this, can you link us to the page in question? So I can inspect and help you out with the write-up. :D

I got it! Thanks, Elvin.

No problem. :)

This archived topic is closed to new replies.