Site logo

[Resolved] reading time php (little help if possible)

Home Forums Support [Resolved] reading time php (little help if possible)

Home Forums Support reading time php (little help if possible)

Viewing 7 posts - 16 through 22 (of 22 total)
  • Author
    Posts
  • #2077719
    David
    Staff
    Customer Support

    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.

    #2078140
    johnaps

    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…

    #2078675
    Tom
    Lead Developer
    Lead Developer

    Strange – what happens if you try it on a test post written in English?

    #2079114
    johnaps

    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!

    #2079125
    David
    Staff
    Customer Support

    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');
    #2080574
    johnaps

    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.

    #2080966
    David
    Staff
    Customer Support

    Phew … thats awesome. Glad we found the solution.

    Reviews are left wp.org 🙂

    https://wordpress.org/support/theme/generatepress/reviews/

Viewing 7 posts - 16 through 22 (of 22 total)
  • You must be logged in to reply to this topic.