Archived topic
reading time php (little help if possible)
21 replies · Started by johnaps on January 9, 2022
Try this function which is going to count the spaces between words instead:
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 = count(preg_split('/s+/', $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');
Note i left the var_dump($word_count); to output the word count.
I tried it that also,
you can see the outcome before_footer section of the page
it is this
int(1)
•Reading time: 1 minutes
http://thefinterest.kinsta.cloud/p/uncategorized/asjalska/#:~:text=int(1),time%3A%201%20minutes
and the post has many many words (it is long) as u can see...
Strange - what happens if you try it on a test post written in English?
The above function seems to work in english.
This is the output for english post
int(425)
•Reading time: 2 minutes
https://thefinterest.kinsta.cloud/p/uncategorized/fund-announced-to-cushion-covid/
Is there anything else we could try for greek u think?
I thank you very much for all the efforts you made thus far!
Having a hard time finding a solution for this, having dug around stack overflow i found this for counting non-word characters, which may work in this instance. Try using it 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 = count(preg_split('/\P{L}/usi', $clean_content, -1, , PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY));
$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');
You CAME through!
Thank you very much for all the efforts, you made this possible for me, so i am much obliged...
I wouldnt have been able to come up with this myself...
A PERFECT SOLUTION FOR COUNTING WORDS FOR GREEK LANGUAGE ON POSTS.
Thank you very much! :)
Please share with me, where i can make a review for gp team support.
Phew ... thats awesome. Glad we found the solution.
Reviews are left wp.org :)