
Since WpRecipes has been launched, I had numerous requests to create a function that can display the word count of a post content. So, here is a function to do it easily.

Since WpRecipes has been launched, I had numerous requests to create a function that can display the word count of a post content. So, here is a function to do it easily.
Simply open your functions.php file and paste this function in it:
function wcount(){
ob_start();
the_content();
$content = ob_get_clean();
return sizeof(explode(" ", $content));
}
Once done, you can call the function within the loop to get the number of words of the current post:
<?php echo wcount(); ?>
This recipe was inspired from this excellent tip.
28 Responses
Why bother with buffering and not just pull post content that WP has prepared?
echo str_word_count($post->post_content);
Sometimes I feel that WP does so much stuff everyone long lost track about what it does and what id doesn’t.
I wanna say just simple thing that, that function is not needed. If anyone wants then alright. Its cool though.
If accurate word counts are required, it should be noted that both methods — in the original post & in the first comment — will count up HTML tags, parameters, and so on as words as well.
Not saying that’s a terrible thing, just adding it as a caveat.
beautifull little code snipped, I thought it will be harder
Thanks!
Once again you did a great job. I always implement your code that you provide every time in your new post. Really you help me a lot buddy..
Both the function and the one fo the first comment are great but I would like to know if there is a function that count the title post.
Awesome post. And it hasn’t updated yet, but I should be the 4000th subscriber. It was 3999 when I did it. Woot!
@Emaus23
Unsuprisingly
that would be
echo str_word_count($post->post_title);
@Rarst: Thanks for excellent tip, I didn’t knew it!
@Jean-Baptiste
Heh, I am spending too much time with WP xref lately.
Things that look simple from outside might actually require five functions and dozen variables. Code base is quite extensive.
@Rarst:
Thanks for your respond, I have a dout when I use
echo str_word_count($post->post_title);
It gives me a count smaller than the real number for example:
Suscribase a la revista
gives me the number: 4; could you tell me what I am doing wrong.
Thank you.
@Emaus23
In your example:
Suscribase(1) a(2) la(3) revista(4)
Seems like 4 should be correct count?
Thank you for such a speedy respond, it seems I didn´t explain my self coorectly when I was asking firstly, I want to count the word in a title because ¨revista¨ has less letter then ¨telecomunicaciones¨. i hope that now you can understand what I am willing to ask.
PD: Sorry for my rusty english, but has been a lot of time since I wrote something in english.
@Emaus23
Not sure I understand. What count do you expect to get for your example? Maybe you mean number of characters, that would be:
echo strlen($post->post_title);
Yes, that it what I wanted. Sorry for not been able to ask properly what I need. I should practice more my english. You are the best.
wpcount is an useful function in Wordpress themes creation.
Although some will think this is not useful, they should think about that twice. There are surely at least few situations when you need this function. For example, if you have writer on your blog, and want to check fast inline how many words does his text contains.
by the way, How can i limit the_content() words or character to the number i want?
Nice function! Well done!
On the next level, What I need is the ability to not allow a post to be published unless it meets a minimum word count….
To get a true word count (i.e. sans HTML tags), would this work:
echo str_word_count(strip_tags($post->post_content));
?
Thanks! Great tips.
If you want this to work on an index page, you can use this:
$exwords = get_the_content();
echo str_word_count(strip_tags($exwords);
This is useful if you want to show an excerpt with a link to the full post content, but only want to show the “read more” tag if the full post exceeds a certain number of words. To do that, you’d go into your index.php template and wrap the normal “read more” link in an ‘if’ statement like so:
55) { ?>
<a href="” rel=”bookmark” title=”">Read more about …
Hmm, my code got stripped – sorry. Not sure how to make it work here. I’ll try again, apologies in advance if this doesn’t work!
Remove the spaces between < and ? to make the open php tags work.
55) { ?>
<a href="” rel=”bookmark” title=”">Read more about …
Trackbacks: