-
AuthorPosts
-
March 9, 2022 at 1:27 pm #1343838
Hi,
I wanted to add the feature Estimated reading time to my posts without using a plugin.
I found the following code in a enfold thread and created as a shortcode./*** Estimated reading time ***/ function reading_time() { $content = get_post_field( 'post_content', $post->ID); $word_count = str_word_count( strip_tags( $content ) ); $readingtime = ceil($word_count / 200); if ($readingtime == 1) { $timer = " minute"; } else { $timer = " minutes"; } $totalreadingtime = $readingtime . $timer; return $totalreadingtime; } add_shortcode( 'rt_reading_time', 'reading_time' );
Everything was working fine, but now the time increased significantly.
For example a post that had around a estimated reading time of 7 mins now is showing me 23 mins.This post is built on the ALB, Is also counting the shortcodes codes of the post?
Can you help me figure this out?
Thank you
CheersMarch 10, 2022 at 4:48 am #1343940Hey Kyle,
Thanks for the link to your function and page, I see that this works correctly on a Classic Editor post, but for an Advanced Layout Builder post the reading time is incorrect. That is because this function is running in PHP on the server side before the Advanced Layout Builder shortcode is executed, so for it to work you would need to have this run after the post content shortcode has been executed, but I found no way to do that.
I recommend using javascript to do this since it runs after the shortcode is executed. I found this Estimated Reading Time script and modified it a little and it seems to work correctly on Classic Editor & Advanced Layout Builder posts.
Try adding this code to the end of your functions.php file in Appearance ▸ Editor://use <div id='r-time'/></div> to display reading time function estimated_reading_time() { ?> <script> //function use to convert character into words function get_text(el) { ret = ""; var length = el.childNodes.length; for(var i = 0; i < length; i++) { var node = el.childNodes[i]; if(node.nodeType != 8) { ret += node.nodeType != 1 ? node.nodeValue : get_text(node); } } return ret; } //main body in which all words exist var words = get_text(document.querySelector('#top.single-post #main>.container_wrap>.container')); var count = words.split(' ').length; //avg reading speed of person 200 word per minute var avg = 200; var counted = count / avg; var maincount = Math.round(counted) //show output of code document.getElementById("r-time").innerHTML = "✮ "+ maincount + " minute read"; </script> <?php } add_action('wp_footer', 'estimated_reading_time');
and add this div to your post as the “shortcode” to display:
<div id='r-time'/></div>
Best regards,
MikeMarch 10, 2022 at 6:42 pm #1344048Hi Mike again :)
Thanks for the quick reply :)
I was able to add the code you have suggested and worked. I just changed the selector from querySelector(‘#top.single-post #main>.container_wrap>.container’) to querySelector(‘#top.single-post #main’) because was only grabbing first color section.
With this change i was able to grab all text (also the footer – if you know a better selector so it not grabs it, please let me know)My main issue is that i have the shortcode on this site and also on many others (and some have hundreds of posts) and to manualy go change it to a replace it with the div is a nightmare and time consuming.
Is there any way to make this javascript to be added to a shortcode to the post?thank you again for the great help.
Cheers
March 11, 2022 at 2:23 am #1344075Hi,
Based on the Advanced Layout Builder post you linked to, inside #main you have 4 color sections, but the second one seems to be the “content” so if this was true for all of them you could use:querySelector('#top.single-post #main #av_section_2')
As for the shortcode, if you already have
[rt_reading_time]
added to many posts you can change the last line in the script:
document.getElementById("r-time").innerHTML = "✮ "+ maincount + " minute read";
to this:var readingTime = document.querySelector("#top.single-post #main"); readingTime.innerHTML = readingTime.innerHTML.replace("[rt_reading_time]", "✮ "+ maincount + " minute read");
in my test this works.
Best regards,
MikeMarch 15, 2022 at 5:02 pm #1344598Hi Mike,
Sorry for the late reply.
Thank you so much for your help was precious ;)
All started working like we wanted.You can close the ticket
Thank you again!
Cheers
March 15, 2022 at 6:15 pm #1344625Hi,
Great, I’m glad that Mike could help you out :-)
I’ll close this thread for now then, please open a new thread if you should have any further questions or problems.
Best regards,
Rikard -
AuthorPosts
- The topic ‘Estimated reading time increased the time’ is closed to new replies.