PHPWordpress

WordPress Word Count Function

By March 8, 2018 No Comments

Ever wondered how to count the number of words in one of your WordPress posts?

Out of the box WordPress doesn’t include a function to do this, but it’s not hard to do.  Just add a custom function to your child theme’s functions.php.

function wordCount() {
    $data = get_post_field( 'post_content', $post->ID );
    $wordCount = str_word_count(strip_tags($data));
    return $wordCount;
}

Now anywhere in your page templates that you would like to echo out your post word count you can using the following command.

echo wordCount();

Leave a Reply