WordPress function to display your posts words count

July 3, 2009 at 8:35 am

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.