Part 4: Neat Little Snippets of Code That Are Useful for WordPress

Okay, so here we move on to Part 4 of my “taking too long to add to this” series :)

I had a client this week who is an author. He does some work on practice lists, and wanted a spot to gather his writing together. The thing that he wanted was something that would count the words within his posts - since many of his lists require practice pieces to be under a certain word limit (or they just want to know how many words there are).

I found a few plugins, but when I went to test them, the word count was always off. One of my test posts was 301 words long, yet the different plugins would vary anywhere form 320 to 350 words. There were different reasons for this, and to me, it wasn’t worth troubleshooting 200 or so lines of code in the plugin to figure out why.

So I wrote my own function instead: and it took all of 10 lines (if you count the commented sections, and the two lines it took to create the function).

So, here we go. My “Count words in the posts” function. I’m not a plugin author, and I’m sure this could be put into a plugin, but it’s such a tiny piece of code that it’s just a lot cleaner to stick it in your functions.php file.


function bb_word_count() {
$pc = get_the_content();
//strip all HTML tags
$spc = strip_tags($pc);
// strip all multiple spaces
$spcns = str_replace(' ',' ',$spc);
// now count the words in the string
$count = str_word_count($spcns);
echo "$count words";
}

As you can see, it’s really simple. I wouldn’t be surprised if it could be done with an even cleaner method to reduce the size. but the actual working lines of code span only 5 lines! The first is to grab the content within your post, the second takes a look at it and strips *all* tags (images, blockquotes, paragraph tags - all the unecessary formattng - this is all about the word count).

The third line the makes sure extra whitespace is stripped. I have a habit of putting in two spaces after a period - I still do it after all of these years, and any of you who’ve taken a typing class know exactly why I do it. The addition of the extra space tends to throw the count off - so this third line takes *any* double spaces and converts them to a single space.

The fourth line then simply counts what’s left - which is the words. The fifth line echoes out the number. Bam, done - and accurate every time.

To further add to what I did, my client wanted to acknowledge where he got his ideas for his practice, and he wanted to place a little section within each post that would describe the purpose of the assignment, and a link back to where he got it. However, the problem was that if he did place this within the content, then my function would count that too.

But this is why I love WordPress so much. :)

My function counts what’s within the_content(); - and WordPress has a nifty little thing called “custom fields” that you can add and manipulate as you see fit. So what I did was create a custom field (called “info”), and in that field, he just put in the information he wanted. Then, in the template file (in his case, single.php), I put this in right above the call to the_content():


<?php
$key="info";
echo get_post_meta($post->ID, $key, true);
} ?>

What this does is look for my custom field (with the key of “info”), and it grabs the post ID of the page you’re on. Then the custom info that’s attached to that post gets put above the post content, to inform the end user of this special information and my count function only counts the actual content - not the custom info.

This is a new site for my client, and he’s given me permisson to link to it for the purposes of this article - but right now there’s no real content there (the site was just put up yesterday) - but I’ll post the link here, because by the time anyone really wants to see it in action, he’ll have some stuff up where you can see how it looks.

So here you go…Andrew J. Wells (But remember, this stuff is set to be seen on a single post page - so you need to click on one of the posts to see it in action!)

Comments

Naomi says...
1

I like the design, Shelly. Simple and clean.

I love custom fields. They are a Wordpress developer’s best friend. I used them on our porfolio page here for the thumbnails: http://www.intuitivedesigns.net/category/portfolio/ It would have been really hard to do what we wanted without them.

Ilona says...
2

Hi! I’m a new reader here.. *waves*

Wow..the word count is a really nifty thing! And it’s very simple too. Just 5 lines of code and you get what you want. ;p

Trackbacks & Pingbacks

Leave a Reply