Post Word Count: How To Display This With WordPress

Steven13 responsesWordPress

If you are a numbers’ guy and would like to display the word count of all you blog posts, there is an easy way to do that. You just need to create a new function in the functions.php file of your own WordPress theme by pasting the following code:

function wordcount(){
    ob_start();
    the_content();
    $postcontent = ob_get_clean();
    return sizeof(explode(" ", $postcontent));
}

Then you need to call the function where you want to display the word count of your posts (for example in your single.php file), by pasting the following code within the loop:

<?php echo wordcount(); ?>

Learn how I made $33,543 online in the past 12 months.

Simply input your best email below to get started.

13 Comments

  1. Michelle

    What if you’re a numbers gal? ;) haha just messing around with you, this is super helpful! For guys and gals alike!

    1. Sarah

      LOL @ numbers gal haha me too!

  2. Shad

    Again I am stealing this coolness for my WordPress. Thanks a ton, man!

  3. Samie

    So this will display the word count to the readers publicly? Or just when I type it out? I definitely do think that could come in handy.. I love the feeling of getting lost while writing and being totally shocked by the word total. It makes me feel like I got a lot accomplished :)

    1. John

      It looks like we will just have to apply it and find out!

  4. Liz

    This looks pretty cool! I wonder what it will look like.. I’m gonna try it!

  5. Jony

    These are some neat tricks that you post on here, I think it comes very much in handy for anyone who has a wordpress site. Very cool of you to post these.

  6. Dana

    I’m definitely applying this to my site. Word count is always a useful figure to measure yourself by.

  7. Buysellwordpress

    It seems to be very simple to do. Thank you so much for sharing it, I will definitely check it out

  8. Anon

    I’m not sure why readers would care about the word count- are they going to be particularly impressed by it? Either way, this is a cool gadget, and it couldn’t hurt to apply it and see the readers’ reactions.

  9. Affmaster

    It definitely couldn’t hurt to have another figure listed, something that could draw a reaction from readers, or something that you yourself can use to analyze your success rate i.e. do posts with higher word counts get more attention or something like that.

  10. Kyle Hotchkiss

    is a buffer count on every page load the best thing to do? I’d think it best to loop this in as a post-save filter and add it to the post metadata just to slim those php compilations a little bit more.

    1. You’re right, it would save a little on PHP processes. However, my goal is to provide a short hack for people who want a quick result, not to turn my readers into PHP gurus.

Learn how I made $33,543 online in the past 12 months.

Simply input your best email below to get started.

No thanks, I have enough money