Limit the Excerpt field in WP-Admin in words

You can use something like jQuery Simply Countable plugin and attach it to excerpt input.

Limit_Excerpt_Words::on_load();

class Limit_Excerpt_Words {

    static function on_load() {

        add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ) );

    }

    static function admin_enqueue_scripts() {

        global $hook_suffix;

        if ( 'post.php' == $hook_suffix || 'post-new.php' == $hook_suffix ) {

            wp_enqueue_script( 'jquery-simply-countable', plugins_url( '/jquery.simplyCountable.js', __FILE__ ), array( 'jquery' ), '0.4.2', true );

            add_action( 'admin_print_footer_scripts', array( __CLASS__, 'admin_print_footer_scripts' ) );
        }
    }

    static function admin_print_footer_scripts() {

        ?>
  <script type="text/javascript">
      jQuery(document).ready(function ($) {

          $('#excerpt').simplyCountable({
              countType:'words', maxCount:5, strictMax:true
          });

      });
  </script>

  <span id="counter" style="display:none;"></span><!-- needs counter to work -->
    <?php
    }
}

PS there is also word-count.js in WP core, but I can’t make sense if it’s as easy to use for blocking stuff.

Leave a Comment