Word Count Plugin for WordPress
Inspired by Chris’ discovery of odd behavior in the wp-word-count plugin I set out to create a version which would not be cheated by occurrences of the word “img” in an entry. I am a coder and web-developer myself and could see myself writing snippets of HTML in my posts.
The solution I found was to run the relevant filters on the content, after I get it back from the database. Using the function apply_filters I was left with a chunk of real HTML. I could strip the tags of this HTML to count the words, and search the HTML for occurences of the <img ... /> tag by searching for the string “<img”.
So there you have it: The Engineered Boulderer’s Word Count Plugin.
Changelog
Version 1.1: Does not count wordpress included smilies as images. Added an argument to allow the plugin to count smilies as images.
Version 1.2: Corrected a bug in the implementation of version 1.1
Documentation
The template function the plugin provides is teb_word_count(display) which accepts two arguments.
display
(boolean) sets whether the function should display the number of words and images, or if it should return them.
- true (default)
- false
count_smilies
(boolean) set whether smilies should be counted as images. If false, the plugin will simply disregard smilies (neither count them as text, nor images).
- true
- false (default)
Disclaimer: This plugin has been tested to work on my test blog and here, but it could break in real life. If you see any problems with my implementation be sure to let me know.
May 6th, 2005 at 10:23 pm
Impressive. I’m not even sure how long it’s been, but you’ve already authored a Wordpress plugin!
May 6th, 2005 at 11:19 pm
Two actually, if you count my category count plugin
I must agree, however, that the first isn’t a proper WordPress plugin as it provided no programming challenges, fortunately this did. I did, however, draw on my experiences from retrofitting a MovableType word count plugin to support counting images.
June 3rd, 2005 at 8:13 pm
It’s working a treat on my site: thanks!
June 14th, 2005 at 10:11 pm
Hi Jonas! Are you from Sweden? Just wondered if it’s possible to make it not count the smileys in the text… If it didn’t count pictures with a URL starting with “wp-images/smilies/” for example… It would have been great
My regards
June 22nd, 2005 at 2:23 pm
doffer,
I’m from Denmark, but often visit Sweden (especially on my climbing trips).
With regards to the smilies, I’ll look at not counting them. Shouldn’t be a huge problem.
July 8th, 2005 at 5:04 pm
[…] Ten tweede is nu te zien hoeveel woorden en/of afbeeldingen een bericht bevat. Zo weet je vantevoren ongeveer hoeveel tijd het lezen van een bericht je zal kosten. Met behulp van Koen werd deze plugin aangepast om zo ook zonder stompzinnige resultaten te werken in de categorie Smalfilm. […]
December 6th, 2007 at 9:07 pm
Where does the word count show up to show the amount of words? I can’t see where to find that information. Any help is most appreciated. Thanks!
December 6th, 2007 at 11:18 pm
webduck,
You must include a call to the function teb_word_count() in the template/theme where you want the word count to occur.