Skip to Content

word_count()

Count words in a post and return the specific count or a range.

PHP November 8, 2018

Usage

PHP
nebula()->word_count($options)

Parameters

$options
(Optional) (Array) Individual options for word count
Default: None

Parameter Notes

Options

id (int)
The post ID to count words in.
Defaults to current post

content (string)
Pass actual content to count rather than a post
Default: false

range (boolean)
Return a range instead of specific word count number
Default: false

Request or provide clarification »

Examples

Count the words in the current post

PHP
nebula()->word_count();

Show a range of the word count for a different post

PHP
nebula()->word_count(array('id' => 123, 'range' => true));

Count the words in a string from a specific custom field

PHP
nebula()->word_count(array('content' => get_field('example')));

Hook custom fields in so they can be counted.

PHP
add_filter('nebula_word_count', function($content, $id, 10, 2){
    $content .= ' ' . get_field('example_field', $id); //Append ACF field to content
    return $content;
});

Additional Notes

This can be useful for use with custom dimensions in Google Analytics on articles.

Be sure to hook in custom fields so they can be included in the word count! It is recommended to add each custom field individually (rather than looping through all fields) to be sure that only contentful output is included.

Source File

Located in /libs/Functions.php on line 841.

2 Hooks

Find these filters and actions in the source code below to hook into them. Use do_action() and add_filter() in your functions file or plugin.

Filters
"pre_nebula_word_count"
"nebula_word_count"
Need a new filter hook? Request one here.

Actions
This function has no action hooks available. Request one?

PHP
    public function word_count($options=array()){
        $override = apply_filters('pre_nebula_word_count', null, $options);
        if ( isset($override) ){return $override;}

        $defaults = array(
            'id' => get_the_id(),
            'content' => false,
            'range' => false, //Show a range instead of exact count
        );

        $data = array_merge($defaults, $options);

        $content = ( !empty($data['content']) )? $data['content'] : get_post_field('post_content', $data['id']);
        $content = apply_filters('nebula_word_count', $content, $data['id']); //Allow additional content to be added to the word count (such as ACF fields)

        $word_count = intval(round(str_word_count(strip_tags($content))));

        if ( is_int($word_count) ){
            if ( !$data['range'] ){
                return $word_count;
            }

            $words_label = __('words', 'nebula');

            if ( $word_count < 10 ){
                $word_count_range = '<10 ' . $words_label;
            } elseif ( $word_count < 500 ){
                $word_count_range = '10 - 499 ' . $words_label;
            } elseif ( $word_count < 1000 ){
                $word_count_range = '500 - 999 ' . $words_label;
            } elseif ( $word_count < 1500 ){
                $word_count_range = '1,000 - 1,499 ' . $words_label;
            } elseif ( $word_count < 2000 ){
                $word_count_range = '1,500 - 1,999 ' . $words_label;
            } else {
                $word_count_range = '2,000+ ' . $words_label;
            }

            return $word_count_range;
        }

        return false;
    }

Override

To override this PHP function, use this hook in your child theme or plugin ("my_custom" can be changed):

PHP
add_filter('pre_nebula_word_count', 'my_custom_word_count', 10, 2); //The last integer must be 1 more than the actual parameters
function my_custom_word_count($null, $options){ //$null is required, but can be ignored
    //Write your own code here

    return true; //Return true to prevent the original function from running afterwords
}

You can completely disable this PHP function with a single line actions:

PHP
 add_filter('pre_nebula_word_count', '__return_false');