Skip to content

Instantly share code, notes, and snippets.

@shizhua
Created August 24, 2015 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shizhua/e75a0083b6734549a18d to your computer and use it in GitHub Desktop.
Save shizhua/e75a0083b6734549a18d to your computer and use it in GitHub Desktop.
Add a Character Counter to Excerpt box
<?php
/**
* Add a Character Counter to Excerpt box
* URL: http://wpsites.org/?p=10503
*/
function excerpt_count_js(){
echo '<script>jQuery(document).ready(function(){
jQuery("#postexcerpt .handlediv").after("<div style=\"position:absolute;top:5px;right:80px;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). (128 Characters MAX)</small></div>");
jQuery("#excerpt_counter").val(jQuery("#excerpt").val().length);
jQuery("#excerpt").keyup( function() {
jQuery("#excerpt_counter").val(jQuery("#excerpt").val().length);
if ( jQuery("#excerpt_counter").val() >= 101 ) {
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');
;?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment