Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

The code below from WPSnipps provides an excerpt character counter, but I'd like to count words instead. Does anybody have an idea of how to do this?

// Excerpt character count
function excerpt_count_js(){
      echo '<script>jQuery(document).ready(function(){
jQuery("#postexcerpt .handlediv").after("<div style=\"position:absolute;top:0px;right:5px;color:#666;\"><small>Excerpt length: </small><input type=\"text\" value=\"0\" maxlength=\"3\" size=\"3\" id=\"excerpt_counter\" readonly=\"\" style=\"background:#fff;\"> <small>character(s).</small></div>");
     jQuery("#excerpt_counter").val(jQuery("#excerpt").val().length);
     jQuery("#excerpt").keyup( function() {
     jQuery("#excerpt_counter").val(jQuery("#excerpt").val().length);
   });
});</script>';
}
add_action( 'admin_head-post.php', 'excerpt_count_js');
add_action( 'admin_head-post-new.php', 'excerpt_count_js');
share|improve this question
2  
This example has to do with jQuery and would be better to ask on stackoverflow.com . –  Wyck Oct 22 '12 at 3:13
2  
Why do you want to reinvent the wheel? WordPress has a word counter already, just look at the regular post editor and use wp-admin/js/word-count.dev.js. –  toscho Oct 22 '12 at 3:16
    
@Wyck I'm not on stackoverflow... I was blocked, ironically, for asking questions like this which the admins decided was a WP question. –  AndrettiMilas Oct 27 '12 at 21:17
1  
The question is asking for the simplest code to get a word count for excerpts in WP, which is relevant for this site. It happens that the question already contains all the wp-specific stuff (i.e. the right hooks, as debugged by others on the original wpsnipp) but still, I'm glad the solution was here on WordPress Answers. –  jeremyclarke Aug 2 '13 at 16:23

2 Answers 2

up vote 3 down vote accepted

Sorry for reading wrong your question @siouxfan45!

here is the right answer: just a little improvement in your code and you can count words!

just change these two lines:

jQuery("#excerpt_counter").val(jQuery("#excerpt").val().length);

to this:

jQuery("#excerpt_counter").val(jQuery("#excerpt").val().split(/\S\b[\s,\.\'-:;]*/).length - 1);

Words with single quote like "don't", "it's", "I'd", "won't"...will count as two! If you want them to count as a single word, then you will want to change the .split() to this:

.split(/\S+\b[\s,\.\'-:;]*/)

Hope I'm right this time!

share|improve this answer
    
This code works wonderfully, thank you. –  AndrettiMilas Oct 27 '12 at 21:19
    
Anytime buddy!! –  KBRckr Nov 13 '12 at 5:48

While implementing this great answer (thanks!) I worked out what I think is a nicer way of displaying the number than the weird inactive field used by the original code. This shows just "Word count: $word_count" right below the textarea. The code below also incorporates KBRckr's code to count contractions (don't) as one word.

Screenshot of what my excerpt word count code gives you

<?php
/**
 * Use jQuery to add a word counter to the excerpt box
 *
 * Should attach to all post screens and indicate the number of words just below the #excerpt textarea
 */
function gv_excerpt_word_count_js() {
      echo '
     <script>jQuery(document).ready(function(){
jQuery("#postexcerpt #excerpt").after("Word Count: <strong><span id=\'excerpt-word-count\'></span></strong>");
     jQuery("#excerpt-word-count").html(jQuery("#excerpt").val().split(/\S+\b[\s,\.\'-:;]*/).length - 1);
     jQuery("#excerpt").keyup( function() {
     jQuery("#excerpt-word-count").html(jQuery("#excerpt").val().split(/\S+\b[\s,\.\'-:;]*/).length - 1);
   });
});</script>
    ';
}
add_action( 'admin_head-post.php', 'gv_excerpt_word_count_js');
add_action( 'admin_head-post-new.php', 'gv_excerpt_word_count_js');
?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.