Site logo

Archived topic

reading time php (little help if possible)

21 replies · Started by johnaps on January 9, 2022

Viewing posts 1–15 of 22

Hello!

I know this maybe out of the scope of support, but i maybe someone will find usefull in the future, and maybe it will be easy to help me, cause i think i am pretty close on achieving this...

I wanted a reading time calculator by minutes.

i have used the following code on my functions.php

function display_read_time() {
    $content = get_post_field( 'post_content', $post->ID );
    $count_words = str_word_count( strip_tags( $content ) );
	
    $read_time = '<span class="rt-suffix">' + ceil($count_words / 250);
    
	 $suffix = ' min</span>'; 
	
    $read_time_output = $read_time . $suffix;

    return $read_time_output;
}

add_shortcode( 'readingtime', 'read_time_sc' );
function read_time_sc() {
    ob_start(); ?>
        	<div class="reading-time">
<?php echo display_read_time(); ?>
</div>
    <?php
    return ob_get_clean();
}

I inserted the function that calculates it and i made a shortcode to use it freely on my site...

I have made a gp element on the left sidebar with this html

<div class="scroll-progress-wrap">
	<div id="circ" class="p0"> 
		[readingtime]
	</div> 
</div>

As you will see it generates a number, but it is always 1, no matter what i do...

Do you have any idea anything i could try?

Thank you in advance!

Hi there,

I suggest doing a var_dump($content) first and see if it's actually showing the proper content to be divided by your wpm. It may not be the one you want to divide.

You can try this one.

    global $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 );

Thank you for the tips!

I tried var_dump($content); and it is returning the content!!

I also tried your code and it still gives me 1 min unfortunately...

Maybe i need to refresh the outcome somehow on already published posts?

I tried it just now again, with $wpm = 1; // How many words per minute.
to test it...

and i get 15 minutes...

so i guess the function only counts 15 words.... :/

Something must be interfering with that function.
I use the same method on a few sites.

Try including var_dump($word_count); in your shortcode like so:

function tu_estimated_reading_time() {
    ob_start();
    $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 ='<div class="read-time"><span class="gp-icon">•</span>Reading time: '. ceil( $word_count / $wpm ) . ' minutes'. '</div>';
    var_dump($word_count);
    return  $time; 
    return ob_get_clean();
}

add_shortcode('tu_reading_time', 'tu_estimated_reading_time');

i tried it, i think the output of the var_dump is
int(15)

you can check it in my page if you want...

Can you try this one?

function tu_estimated_reading_time() {
    ob_start();
    $raw_content = get_the_content( get_the_ID() );
    $decode_content = html_entity_decode( $raw_content );
    $filter_shortcode = do_shortcode( $decode_content );
    $strip_tags = wp_strip_all_tags( $filter_shortcode, true );
    $count = str_word_count( $strip_tags );
    $wpm = 250;
    $readtime = ceil( $count / $wpm );
    $time ='<div class="read-time"><span class="gp-icon">•</span>Reading time: '. $readtime . ' minutes'. '</div>';
    return  $time; 
    return ob_get_clean();
}

add_shortcode('tu_reading_time', 'tu_estimated_reading_time');

I tried it. Check it bellow.
If you search the read-time element you will see that it is still 1 minute, unfortunately...

Strange,

I wonder if it has something to do w/ the language characters used as I've tried my suggestion and David's on a sandbox site and they were both working.

But the tests were both for english contents using latin characters.

Any chance you can try making an english content and see if it works there? You can try making dummy english posts using FakerPress plugin.

i think you are right! check the link in the private info

it shows 6 minutes...

How could i overcome this... or research for this somehow?

Thank you for all the help isolating this matter

Should i use it like this?

function tu_estimated_reading_time() {
ob_start();
$raw_content = get_the_content( get_the_ID() );
$decode_content = html_entity_decode( $raw_content, ENT_COMPAT, 'ISO-8859-7' );
$filter_shortcode = do_shortcode( $decode_content );
$strip_tags = wp_strip_all_tags( $filter_shortcode, true );
$count = array_count_values( $strip_tags );
$wpm = 250;
$readtime = ceil( $count / $wpm );
$time ='

'. $readtime . ' min'. '

';
return $time;
return ob_get_clean();
}

add_shortcode('tu_reading_time', 'tu_estimated_reading_time');`

You can try that or do str_word_count as David suggested for the striped tagged content which is $strip_tags and do a var_dump of just to know if it actually does what's intended.

I'd consider doing a var_dump for all variables to be sure where the issue starts.

I tried

function tu_estimated_reading_time() {
ob_start();
$raw_content = get_the_content( get_the_ID() );
$decode_content = html_entity_decode( $raw_content, ENT_COMPAT, ‘ISO-8859-7′ );
$filter_shortcode = do_shortcode( $decode_content );
$strip_tags = wp_strip_all_tags( $filter_shortcode, true );
$count = array_count_values( $strip_tags );
$wpm = 250;
$readtime = ceil( $count / $wpm );
$time =’

‘. $readtime . ‘ min’. ‘
‘;
return $time;
return ob_get_clean();
}

add_shortcode(‘tu_reading_time’, ‘tu_estimated_reading_time’);

but i got 0 minutes as a result.

and i tried changing the code to this also

function tu_estimated_reading_time() {
    ob_start();
    $raw_content = get_the_content( get_the_ID() );
    $decode_content = html_entity_decode( $raw_content );
    $filter_shortcode = do_shortcode( $decode_content );
    $strip_tags = wp_strip_all_tags( $filter_shortcode, true );
    $count = str_word_count( $strip_tags );
    $wpm = 250;
    $readtime = ceil( $count / $wpm );
    $time ='<div class="reading-time">'. $readtime . ' min'. '</div>';
    return  $time; 
    return ob_get_clean();
}

add_shortcode('tu_reading_time', 'tu_estimated_reading_time');

but i got 1 minute again...

Lastly i did var_dump for all variables.
in this order

function tu_estimated_reading_time() {
    ob_start();
    $raw_content = get_the_content( get_the_ID() );
    $decode_content = html_entity_decode( $raw_content );
    $filter_shortcode = do_shortcode( $decode_content );
    $strip_tags = wp_strip_all_tags( $filter_shortcode, true );
    $count = str_word_count( $strip_tags );
    $wpm = 250;
    $readtime = ceil( $count / $wpm );
    $time ='<div class="reading-time">'. $readtime . ' min'. '</div>';
	var_dump($wpm);
	var_dump($raw_content);
    var_dump($decode_content);
    var_dump($filter_shortcode);
    var_dump($strip_tags);
    var_dump($count);
	
    return  $time; 
    return ob_get_clean();
}

add_shortcode('tu_reading_time', 'tu_estimated_reading_time');

and you can see the result in my link in the before_footer section, i added the shortcode only there, with a hook, so it will be easily visible for you.

In my first try i dint find anything, i will try looking again to understand.

Please if you take a look also though, i would really appreciate it!

This archived topic is closed to new replies.