Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/** 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