I would like to get some help in Limiting the content entered by editor or admin in the WP admin editor window by words and based on the categories as well for example when posting in political category i have a limit of 400 words, in sports 450, in entertainment 500 words etc..


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 ) {
                } else {

    <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; }

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.

| improve this answer | |
  • For me, tinyMCE.activeEditor is NULL at time of execution of that script. Is there another, better way to embed it? And yes, it is 2020 but I am still using the "classic" editor. – Felix May 13 at 16:02
  • 1
    @Felix I get it :) we have many clients that still won't use Gutenberg. So I don't know off the top of my head why this wouldn't work but my guess is its something to do with how the tinyMCE editor is being initialised now that its a separate plugin or its a newer version of tinyMCE? So I would check to see if you can find out how its being used now. Check the tinyMCE docs for more info – yobddigi May 14 at 9:50
  • Using window.onload = .... { tinyMCE.editors[0]... } seems to work for me. If I have to touch my code again (quite likely) I would add it as a separate answer (I also do something different, displaying the max words in statusbar). But feel free to dig and correct me - it is kind-of working for now and I'll have to see which new specs come up. – Felix May 14 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.