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');
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:
to this:
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:Hope I’m right this time!
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.