Skip to content

Instantly share code, notes, and snippets.

@alexwcoleman
Created September 12, 2017 18:36
Show Gist options
  • Save alexwcoleman/3bb00b969ab4c47a1b34ceeb13dc211b to your computer and use it in GitHub Desktop.
Save alexwcoleman/3bb00b969ab4c47a1b34ceeb13dc211b to your computer and use it in GitHub Desktop.
Character Counter in Custom Post Type excerpts
function CPT_excerpt_counter(){
// make sure to change all occurrences of the counter limit.
if ('YOUR_POST_TYPE' == get_post_type()) {
echo '<script>jQuery(document).ready(function(){
jQuery("#postexcerpt .handlediv").after("<div style=\"position:absolute;top:12px;right:34px;color:#666;\"><small>Excerpt length: </small><span id=\"excerpt_counter\"></span><span style=\"font-weight:bold; padding-left:7px;\">/ 100</span><small><span style=\"font-weight:bold; padding-left:7px;\">character(s).</span></small></div>");
jQuery("span#excerpt_counter").text(jQuery("#excerpt").val().length);
jQuery("#excerpt").keyup( function() {
if(jQuery(this).val().length > 100){
jQuery(this).val(jQuery(this).val().substr(0, 100));
}
jQuery("span#excerpt_counter").text(jQuery("#excerpt").val().length);
});
});</script>';
}
}
add_action( 'admin_head-post.php', 'CPT_excerpt_counter');
add_action( 'admin_head-post-new.php', 'CPT_excerpt_counter');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment