- This topic has 21 replies, 4 voices, and was last updated 4 years, 2 months ago by
David.
-
AuthorPosts
-
January 9, 2022 at 2:04 pm #2074646
johnaps
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!
January 9, 2022 at 5:14 pm #2074754Elvin
StaffCustomer SupportHi 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 );January 10, 2022 at 12:21 am #2074962johnaps
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?
January 10, 2022 at 4:37 am #2075211David
StaffCustomer SupportHi there,
have you tried the function Tom wrote here:
https://generatepress.com/forums/topic/read-time-for-a-blog-post/page/2/#post-1968767
January 10, 2022 at 6:04 am #2075299johnaps
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…. :/
January 10, 2022 at 6:27 am #2075321David
StaffCustomer SupportSomething 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');January 10, 2022 at 12:36 pm #2075845johnaps
i tried it, i think the output of the var_dump is
int(15)you can check it in my page if you want…
January 10, 2022 at 5:30 pm #2076040Elvin
StaffCustomer SupportCan 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');January 10, 2022 at 11:53 pm #2076213johnaps
I tried it. Check it bellow.
If you search theread-timeelement you will see that it is still 1 minute, unfortunately…January 11, 2022 at 12:22 am #2076225Elvin
StaffCustomer SupportStrange,
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.
January 11, 2022 at 7:15 am #2076574johnaps
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
January 11, 2022 at 8:38 am #2076838David
StaffCustomer SupportNot sure but maybe instead of using
str_word_count– this method which counts spaces would work:https://coderedirect.com/questions/5941/str-word-count-for-non-latin-words
January 11, 2022 at 1:24 pm #2077149johnaps
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’);`
January 11, 2022 at 8:09 pm #2077367Elvin
StaffCustomer SupportYou can try that or do
str_word_countas David suggested for the striped tagged content which is$strip_tagsand 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.
January 12, 2022 at 12:11 am #2077504johnaps
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 orderfunction 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_footersection, 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!
-
AuthorPosts
- You must be logged in to reply to this topic.