# Word Count Shortcode

> Adds a [word_count] shortcode that prints how many words the current post contains, handy for long-form guides and writing portfolios.

- Type: PHP
- Runs: Run everywhere
- Category: [Content](https://wpide.com/library/category/content/)
- Tags: shortcode, word-count, posts
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/word-count-shortcode/

## Code

```php
add_shortcode( 'word_count', function ( $atts ) {
    $atts    = shortcode_atts( array( 'id' => 0 ), $atts, 'word_count' );
    $post_id = $atts['id'] ? absint( $atts['id'] ) : get_the_ID();

    if ( ! $post_id || ( ! is_post_publicly_viewable( $post_id ) && ! current_user_can( 'read_post', $post_id ) ) ) {
        return '';
    }

    $text  = wp_strip_all_tags( strip_shortcodes( get_post_field( 'post_content', $post_id ) ) );
    $count = count( preg_split( '/\s+/u', trim( $text ), -1, PREG_SPLIT_NO_EMPTY ) );

    return '<span class="wpide-word-count">' . esc_html( number_format_i18n( $count ) ) . '</span>';
} );
```

## Notes

Use [word_count id="123"] to show the count of another post.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Word Count Shortcode" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
