Skip to content

Instantly share code, notes, and snippets.

@dwesolowski
Created September 3, 2018 08:56
Show Gist options
  • Save dwesolowski/de2da42734dca9f9622a9877170230c2 to your computer and use it in GitHub Desktop.
Save dwesolowski/de2da42734dca9f9622a9877170230c2 to your computer and use it in GitHub Desktop.
/** Add word count to excerpts */
function excerpt_count_js() {
if ('page' != get_post_type()) {
?>
<style>.span-excerpt_counter{font-size:12px;font-weight:normal;float:right;}</style>
<?php
echo '<script>jQuery(document).ready(function($) {
$("#postexcerpt .hndle").append("<span class=\"span-excerpt_counter\">Excerpt length: <span id=\"excerpt_counter\"></span><span> / 10 </span><span>character(s).</span></span>");
$("span#excerpt_counter").text($("#excerpt").val().length);
$("#excerpt").keyup( function() {
if($(this).val().length >= 10) {
$("span#excerpt_counter").css("color", "red");
$(this).val($(this).val().substr(0, 10));
}else{
$("span#excerpt_counter").css("color", "");
}
$("span#excerpt_counter").text($("#excerpt").val().length);
});
});</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