Community & Support

Display word count for stories

Hello,

Is there an exiting option to display the number of words in a story? I looked at the options and couldn't find it.

I'd settle for it showing under RECENT POSTS, in the Read Comments line after story, etc. Just somewhere it is displayed.

Thanks!

Comments

You can do this in the

You can do this in the theme.

In a PHPTemplate theme, put the following code in node.tpl.php where you want the word count to display:

<?php
$wordcount
= count(explode(" ", strip_tags(trim($node->body))));
print
$wordcount;
?>

If you just want this to display in stories, copy the exising node.tpl.php file to node-story.tpl.php, and insert the above code.

(Username formerly my full name, Richard Eriksson.)

Thanks! I will give that

Thanks! I will give that shot!

Ok, I did that.

Ok, I did that.

It didn't turn out right. lol

Couple of issues:

1) On the front page the word count shows as 1 no matter what.

2) I can't figure out where to put it so that it goes after this:

» add new comment | 7 reads | unsubscribe post

Any ideas?

Thanks again!

Here's one way

By this time, you might have figured it out already, but just in case:

In node.tpl.php, add this line:

<?php $wordcount = " | ".count(explode(" ", strip_tags(trim($content))))." words" ; ?>

I added it on a new line just after line 4, which has "}?>" in my version of the file. Then, make the following changes to the line that starts with "<?php if ($links)"

<?php if ($links) { ?> <div class="links">&raquo; <?php print $links; print $wordcount ?><?php }; ?>

And that should take care of both issues. One caveat -- if you have any modules activated that add stuff to the $content block (e.g. the nodevote module), it will throw off the count.

Drupal 6 Word count

For Drupal 6.x, you just have to modify the code slightly:

In node.tpl.php, add this line:

<?php $wordcount = " | ".count(explode(" ", strip_tags(trim($content))))." words" ; ?>

Add it, as above, on a new line just after line 4, which has }?>

Then, make the following changes to the line that starts with <?php if ($links)

<?php if ($links) { ?> <?php print $links; print $wordcount ?><?php }; ?>

(Basically, I just removed the <div class="links">&raquo; tag from the Drupal 5 version)

As before, if you just want this to display in stories, copy the exising node.tpl.php file to node-story.tpl.php, and insert the above code.

Thanks

Thanks for posting this, working well on my side.

Tim

Very handy but it might not be 100% accurate

Thanks for the code! What is a bit weird is that the count is not exactly the same as in both mircosoft word and open office writer. I made sure I only counted the words from the $content variable. Can't find a reason for this, It is pretty accurate though.
For example for one page the php code shows 184 words and word/writer show 189 words. (I had to copy paste the text in notepad first otherwise word/writer displayed completely wrong word counts) I am using this in the Ubiquity theme so maybe it has something to do with the theme. Does anyone else notices a discrepancy?

Drupal 6

Proper way to do this is more like...

In template.php:

<?php
function yourtheme_preprocess_node(&$vars) {
 
$body = $vars['node']->content['body'];
 
$vars['wordcount'] = count(explode(" ", strip_tags(trim($body['#value']))));
}
?>

And in node.tpl.php something like:

  <div class="wordcount">
    <strong>Word count:</strong> <?php print $wordcount; ?>
  </div>

Better still, put the above in a tiny module. That's what I did. Will publish if I get chance. Only code in the module (called wordcount) is this:

<?php
function wordcount_preprocess_node(&$vars) {
 
$body = $vars['node']->content['body'];
 
$vars['wordcount'] = count(explode(" ", strip_tags(trim($body['#value']))));
}
?>

Another option is to save the

Another option is to save the word count when saving the node rather than recalculating it each time:

<?php
function od_tweaks_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if (
$op == "insert") { // Save the word count
   
$body = $node->body;
   
$word_count = count(explode(" ", strip_tags(trim($body))));
   
$nid = $node->nid;
   
db_query("INSERT INTO {od_tweaks} (nid, wordcount) VALUES ($nid, $word_count)");
  } else if (
$op == "update") {
   
$body = $node->body;
   
$word_count = count(explode(" ", strip_tags(trim($body))));
   
$nid = $node->nid;
   
db_query("UPDATE {od_tweaks} SET wordcount = $word_count WHERE nid = $nid");
  }
}
?>