Last active
May 7, 2019 01:31
-
-
Save hearvox/46e443e3d5cba71c298604b6c8925e0e to your computer and use it in GitHub Desktop.
Add a Character Counter to Excerpt box in Edit Post screen (with suggested range).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add a Character Counter to Excerpt box in Edit Post screen. | |
* | |
* Includes suggested range. | |
* Char-count number is color red when outside range and green within. | |
* | |
* @link http://wpsites.org/add-character-counter-excerpt-box-10503/ | |
*/ | |
function my_excerpt_count_js() { | |
echo '<script>jQuery(document).ready(function(){ | |
// Classic Editor | |
if ( jQuery("#excerpt").length) { | |
jQuery("#postexcerpt .handlediv").after("<div style=\"position:absolute;top:5px;right:80px;color:#666;\"><input type=\"text\" value=\"0\" maxlength=\"3\" size=\"3\" id=\"excerpt_counter\" readonly=\"\" style=\"background:#fff;\"> <small>characters (best for HE: 120-240)</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() < 120 || jQuery("#excerpt_counter").val() > 240 ) { | |
jQuery("#excerpt_counter").css("color","red"); | |
} else { | |
jQuery("#excerpt_counter").css("color","green"); | |
} | |
}); | |
} | |
// Gutenberg editor | |
if ( jQuery("#editor-post-excerpt-0").length) { | |
jQuery("#editor-post-excerpt-0").parent().after("<div style=\"\"><input type=\"text\" value=\"0\" maxlength=\"3\" size=\"3\" id=\"excerpt_count\" readonly=\"\" style=\"background:#fff;\"> <small>characters (best for HE: 120-240)</small></div>"); | |
jQuery("#excerpt_count").val(jQuery("#editor-post-excerpt-0").val().length); | |
jQuery("#editor-post-excerpt-0").keyup( function() { | |
jQuery("#excerpt_count").val(jQuery("#editor-post-excerpt-0").val().length); | |
if ( jQuery("#excerpt_count").val() < 120 || jQuery("#excerpt_count").val() > 240 ) { | |
jQuery("#excerpt_count").css("color","red"); | |
} else { | |
jQuery("#excerpt_count").css("color","green"); | |
} | |
}); | |
} | |
});</script>'; | |
} | |
add_action( 'admin_head-post.php', 'my_excerpt_count_js'); | |
add_action( 'admin_head-post-new.php', 'my_excerpt_count_js'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment