Checking to length and limiting it is no problem, but as you set the category when you save the post setting different limits per category would be a bit trickier. Below are the steps you would need to take:
- checking the
$post
var - make it mandatory that a user selects a category
- seeing what cat the user chose
- setting custom category meta data containing the max length for that category
- and then only submitting if it is less and if it isn’t throw an error
But as I said just setting a general limit is fairly simple. Here is a simple snippet of PHP and javascript that retrieves the current editor on the page, counts the number of characters in the editor and displays a warning if the character count exceeds a certain limit. Using a prominent warning instead of truncating content automatically offers a much better usability. You could always make this into a plugin to make things simpler if not make sure you are using a child theme.
I haven’t tested this, but it should work fine.
add_action( 'admin_print_footer_scripts', 'check_textarea_length' );
function check_textarea_length() {
?>
<script type="text/javascript">
jQuery( document ).ready( function($) {
var editor_char_limit = 50;
$('#post-status-info').append('<span class="word-count-message">Reduce word count!</span>');
tinyMCE.activeEditor.onKeyUp.add( function() {
// Strip HTML tags, WordPress shortcodes and white space
editor_content = this.getContent().replace(/(<[a-zA-Z\/][^<>]*>|\[([^\]]+)\])|(\s+)/ig,'');
if ( editor_content.length > editor_char_limit ) {
$('#post-status-info').addClass('toomanychars');
} else {
$('#post-status-info').removeClass('toomanychars');
}
});
});
</script>
<style type="text/css">
.word-count-message { font-size:1.1em; display:none; float:right; color:#fff; font-weight:bold; margin-top:2px; }
.toomanychars { background:red; }
.toomanychars .word-count-message { display:block; }
</style>
<?php
}
If using this in production, remember to move both Javascript and CSS into files and use appropriate admin hooks for adding them only on pages that display the editor you need.