How to: WordPress Count Words in a Post

Here is sample code I use to count words in a post ( I found it somewhere while searching something else). Put the code in functions.php (no need to write plugin)

function blog_post_wordcount() {
        global $page, $pages;
        if ( !function_exists('str_word_count') ) {
                return str_word_count(strip_tags($pages[$page-1]));
        } else {
                return count(explode(" ",strip_tags($pages[$page-1])));

You can call blog_post_wordcount() from template. Following code will display ad only if a post has more than 100 words:

if ( $words >= 100 ) { show_ad(): }

Author: admin

I like chocolate, gadgets, open source software, photography, traveling and all shades of green colors. I love spending time with fun loving friends and family members. This is my own online journal.

4 thoughts on “How to: WordPress Count Words in a Post”

  1. nice, I have a summary of articles on my main page and this will help readers decide if they have time to read the whole thing.

  2. I am looking for a php or java script which allow message to be written back above the message box together with the message date on the left? Can you help? urgently

  3. What I did not understand is

    if ( !function_exists(‘str_word_count’) ) {
    return str_word_count(strip_tags($pages[$page-1]));

    If function does not exist, use it?

Leave a Reply

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