Add Character Counter To Excerpt Box In WordPress

WordPress displays a nice word counter below the post content area to let you know how long the post you are working on is. But it has no such counter for the excerpt area.

If you are somewhat into the SEO field then you would know that having the excerpt long enough is not good from SEO perspective. Some Search Engine specialist suggest that you should have your post excerpt 130-160 letters long. If it goes beyond 160, you are giving away too much description that will not fit in search engine’s description area.

But counting excerpt length manually is again a huge problem, and using some other text editor is again time-consuming. To make this process easier, we have a small snippet, which when added to “functions.php“, displays a letters counter above the excerpt meta box.

The counter will automatically turn red if you exceed the character limit of 160 characters. It won’t trim your excerpt, but is just an indication that you are exceeding the suggested character limit.

function excerpt_count_js() {   echo '<script>     jQuery(document).ready(function() {       jQuery("#postexcerpt .inside").prepend("<div style=\"margin: 5px 0;\" color:#666;\"><small>Letter Count: <span style=\"font-weight:bold; padding: 0 5px;\" id=\"excerpt_counter\"></span></small></div>");       jQuery("#excerpt_counter").val(jQuery("#excerpt").val().length);       jQuery("#excerpt").keyup( function() {         excerpt_color();       });     excerpt_color();   }); function excerpt_color() {   jQuery("#excerpt_counter").html(jQuery("#excerpt").val().length);   if ( parseInt(jQuery("#excerpt_counter").html()) >= 160 ) {     jQuery("#excerpt_counter").css("color", "red");   } else {     jQuery("#excerpt_counter").css("color", "green");   } } </script>'; } add_action( 'admin_head-post.php', 'excerpt_count_js'); add_action( 'admin_head-post-new.php', 'excerpt_count_js');
Related Post