Aleš Sýkora wrote

Shortcode to display word count or reading time

1. 7. 2019 in

This shortcode &lsqbisword-count&rsqb will display a count of words for whatever content is passed, and can optionally display an estimated reading time.

/**
 * Register word-count shortcode
 */
add_shortcode('word-count', function ($atts = [], $content = null) {

	// Defaults
    $atts = shortcode_atts(
        array(
			'time' => false,
			'pace'	=> 150
        ),
        $atts
    );

	if ( isset( $content ) ){
		$content = apply_filters( 'the_content', $content );
		$content = wp_strip_all_tags( $content );
		$word_count = count( preg_split( '/s+/', $content ) );
		$reading_time = ceil( $word_count / $atts['pace'] );

		return ( $atts['time' ] ) ? $reading_time : $word_count;
	}
});

Use on its own to output word count, e.g. in a template to output the word count of the post content, or pass ‘time’ and optional ‘pace’ parameters to output estimated reading time (pace defaults to 150 words per minute can be changed).

Usage of shortcode:

Word count: [word-count][wpv-post-body view_template="None"][/word-count]
Read time: [word-count time=true pace=100][wpv-post-body view_template="None"][/word-count]

Snipped Created By: Nigel Anderson

for Toolset Views

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

More from category
Aleš Sýkora wrote
Featured Image missing after WordPress update
22. 3. 2021 in
Using WordPress + Oxygen builder and can not found featured image in post/page settings? It happend to me after WordPress 5.7 update. If you didn’t disabled it in gutenberg settings, then you might have same problem as me. How to fix it? Install/re-Install theTwenty Twenty-One theme to your website and your featured images will be […]
Read More
Aleš Sýkora wrote
Auto login after creating new user with Toolset user form
11. 7. 2019 in
Snippet from our fan.
Read More
Aleš Sýkora wrote
Custom WordPress login logo
15. 1. 2021 in
Would you like to have your brand logo instead of default WordPress logo at your login page? Use this snippet in your custom funcionality plugin or in code snippets etc. This snippet changes the default logo and add link to the homepage. It also add custom Title for mouse hover over the logo.
Read More
magnifiercross linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram