Created and Installed Words in Post Plugin

I’m proud to present my first WordPress plugin, Words in Post. All it does is count up the number of the words in a post and spit them out. By sticking this code in the index.php template of WordPress, I can create a title line that looks like this:

Geekery � 49 words � lee @ 2:50 am

This is better than using the following simple grab that I had heard about here.

<?php echo(str_word_count(get_the_content())); ?> words

The preceding doesn’t grab anything past the <!--more--> and counts tags incorrectly.

Thanks to Nick Momrik for the inspiration.

 

1-15-05 update: Version 1.1. Thanks to Alderete’s suggestion, the function returns a number instead of echoing. See the php for instructions.

Download Words in Post plugin version 1.1 here. Rename it from .phps to .php and run with it.

14 Comments

  1. craig says:

    I use a word count plugin from way back when. It also does a sum of the total words posted. Does yours also do a total of all posts?
    I will try out your plugin to see if there is a difference between it and the one I’m using now. Thanks for putting this together and sharing it with the WP community!

  2. Lee says:

    Mine doesn’t but get_post_word_count from Nick Momrik does. You can find it at http://mtdewvirus.com/wp-hacks/ and was the inspiration for this script.

  3. Alderete says:

    I might suggest that the plug-in return the number of words, rather than output it, so that you can pass the string along to other functions, for example, so it can be put in the “Read More” string, e.g.:

    < ?php the_content('Read the rest of this entry (' . words_in_post() . ' words) »'); ?>

  4. Lee says:

    A fine suggestion, Alderete. Done! Version 1.1 is created. See the post.

    Hmm. To make it do correctly what you’re suggesting, we need a function that looks for the MORE bar and counts the text -after- it. Not hard… I’ll see what I can do. Or give it a shot yourself.

  5. Alderete says:

    Oh, sorry, I wasn’t clear about what to count. In my usage snippet above, it’s just getting the article total word count, and that’s just fine for my purposes. I don’t actually want to count just the words after the < ! --more-->, but the count for the entire article.

  6. Lee says:

    Well good then! I’m still going to try and make a “words after more” plugin. :-)

  7. Jin says:

    Hi,

    I tried out your plugin and noticed that it doesn’t count individual letters as words. For example:

    “I love goats” would count as two words, as the word “I” is not counted. Any chance you can fix this?

  8. Lee says:

    Hmm. I’ll look into it. But no promises.

  9. jon says:

    for whatever reason, i had to modify the plugin to pull from the proper db by adding $tableposts variable as a global, and using it as the FROM instead of $wpdb->posts. i’m using WP 1.2.2.

    works great though, after that fix. thanks

  10. Why not just use
    str_word_count(strip_tags($post->post_content))

    It works great and accurately as far as I can see. Additionally it doesn’t do an extra SQL query like your plugin to get the post content. The post_content is already available from $post.

  11. carl says:

    What does this mean

    Why not just use
    str_word_count(strip_tags($post->post_content))

    It works great and accurately as far as I can see. Additionally it doesn’t do an extra SQL query like your plugin to get the post content. The post_content is already available from $post.

    How do i use this?

  12. carl says:

    alos, I am getting this error like 6 times int he admin panel

    Warning: Cannot modify header information – headers already sent by (output started at /home/mydomain/public_html/revivalblog/wp-content/plugins/word-count.php:26)

  13. Lee says:

    >Why not just use str_word_count(strip_tags($post->post_content))
    reread my initial post.

    >this error like 6 times
    You might have inserted an extra space or carriage return (or are missing something) at the beginning of the script. It works peachy-keen for me.

  14. Kimberley says:

    thank you SO much for the code to put the count words on my site. it’s greatly appreciated.
    take care.