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

sillygwailo’s picture

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:

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

pgrote’s picture

Thanks! I will give that shot!

pgrote’s picture

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!

Robert Daeley’s picture

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.

markfullmer’s picture

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.

tjharman’s picture

Thanks for posting this, working well on my side.

Tim

Matti303’s picture

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?

greg.harvey’s picture

Proper way to do this is more like...

In template.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:

function wordcount_preprocess_node(&$vars) {
  $body = $vars['node']->content['body'];
  $vars['wordcount'] = count(explode(" ", strip_tags(trim($body['#value']))));
}
Tom Ash’s picture

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

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");
  }
}
designarti’s picture

Is there a way to print word count for the body field after node save in D7?

Think. Observe. Think. Conclude. Then talk.
www.globi.ro - just another travel guide