- This topic has 21 replies, 4 voices, and was last updated 4 years, 2 months ago by
David.
-
AuthorPosts
-
January 12, 2022 at 4:26 am #2077719
David
StaffCustomer SupportTry 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.January 12, 2022 at 8:28 am #2078140johnaps
I tried it that also,
you can see the outcomebefore_footersection of the page
it is thisint(1) •Reading time: 1 minuteshttp://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…
January 12, 2022 at 9:12 pm #2078675Tom
Lead DeveloperLead DeveloperStrange – what happens if you try it on a test post written in English?
January 13, 2022 at 7:30 am #2079114johnaps
The above function seems to work in english.
This is the output for english post
int(425) •Reading time: 2 minuteshttps://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!
January 13, 2022 at 7:47 am #2079125David
StaffCustomer SupportHaving 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');January 14, 2022 at 11:55 am #2080574johnaps
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.
January 15, 2022 at 3:03 am #2080966David
StaffCustomer SupportPhew … thats awesome. Glad we found the solution.
Reviews are left wp.org 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.