Post Pic

WordPress function to display your posts words count

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

Jul 03 2009 17:52

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. :)

Jul 03 2009 18:21

I wanna say just simple thing that, that function is not needed. If anyone wants then alright. Its cool though.

Jul 03 2009 23:57

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.

Jul 04 2009 14:53

beautifull little code snipped, I thought it will be harder :) Thanks! :)

Jul 04 2009 17:10

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..

Jul 05 2009 00:52

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.

Jul 05 2009 02:06

Awesome post. And it hasn’t updated yet, but I should be the 4000th subscriber. It was 3999 when I did it. Woot!

Jul 05 2009 08:06

@Emaus23

Unsuprisingly :) that would be

echo str_word_count($post->post_title);

Jul 05 2009 08:16

@Rarst: Thanks for excellent tip, I didn’t knew it!

Jul 05 2009 13:35

@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.

Jul 05 2009 15:44

@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.

Jul 05 2009 15:53

@Emaus23

In your example:

Suscribase(1) a(2) la(3) revista(4)

Seems like 4 should be correct count?

Jul 05 2009 16:54

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.

Jul 05 2009 18:25

@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);

Jul 05 2009 20:43

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.

Jul 06 2009 20:58

wpcount is an useful function in Wordpress themes creation.

Jul 07 2009 19:45

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.

Jul 14 2009 03:10

by the way, How can i limit the_content() words or character to the number i want?

Sep 02 2009 16:02

Nice function! Well done!

Oct 03 2009 17:43

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….

Oct 28 2009 13:53

To get a true word count (i.e. sans HTML tags), would this work:

echo str_word_count(strip_tags($post->post_content));

?

Oct 29 2009 20:47

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 …

Oct 29 2009 20:51

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:

Leave a Comment

* Name, Email, Comment are Required