WordPress: aggiungere un contatore di caratteri al box del sommario dei post con jQuery
Una semplice aggiunta in jQuery per avere un contatore di caratteri sul box del sommario dei post.
Il sommario dei post di WordPress non dovrebbe superare una certa lunghezza in modo da essere appetibile a livello SEO. Possiamo aggiungere un contatore di caratteri in jQuery al box del sommario. Vediamo come fare.
Aggiungete il seguente codice al file functions.php:
function excerpt_count_js(){
echo ' <script>jQuery(document).ready(function(){
jQuery("#postexcerpt .handlediv").after("<div style=\"position:absolute;top:0px;right:5px;color:#666;\"><small>Lunghezza del sommario: </small><input type=\"text\" value=\"0\" maxlength=\"3\" size=\"3\" id=\"excerpt_counter\" readonly=\"\" style=\"background:#fff;\"> <small>caratteri.</small></div>");
jQuery("#excerpt_counter").val(jQuery("#excerpt").val().length);
jQuery("#excerpt").keyup( function() {
jQuery("#excerpt_counter").val(jQuery("#excerpt").val().length);
});
});</script>';
}
add_action( 'admin_head-post.php', 'excerpt_count_js');
add_action( 'admin_head-post-new.php', 'excerpt_count_js');
Ovviamente potete modificare la struttura HTML generata da jQuery.