HowTo – Displaying The Word Count On Your WordPress Posts

Filed under: Wordpress Tips

wordpress-logoIt is really difficult to say how word count effects your search engine placement but many people say that a minimum of 400 words to around 1,000 words per page is enough for the average bot to read and then index your pages.

While editing your pages in the WordPress visual editor you can look to the bottom left of the editor window to view your approximate word count for the page you are currently writing. This is important for writers but if you are an editor reviewing your site you may want to do it from the front end and not open every page for editing.

In this case you can add a word count to your WordPress posts and quickly check if your writers are keeping to your standard of content generation.

To include a word count on your Posts you need to edit your single.php file and for pages you edit your page.php file. This may change depending on the theme you are using but the area that you need to place the php code to count the words on your page is within the WordPress Loop.

[php]
<?php function count_words($str){
$words = 0;
$str = eregi_replace(" +", " ", $str);
$array = explode(" ", $str);
for($i=0;$i < count($array);$i++)
{
if (eregi("[0-9A-Za-zÀ-ÖØ-öø-ÿ]", $array[$i]))
$words++;
}
return $words;
}?>

Word count: <?php echo count_words($post->post_content); ?>

[/php]

You can place the function above your loop but the line that says Word count: and then code should be placed within your loop.

I like to place it near the meta information that displays categories and tags.

You may also want to place the function into your functions.php file or in another file that you call to build your pages.

The word count code is pretty simple but you may need to adjust it to fit a language other then english.