Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is it possible to limit the Excerpt field on the post page in words? Note that I am aware that it's possible to echo the excerpt and limit it in words, but I want the field itself to limit the amount of words, similar to how character limits on text-areas work.

Is such thing possible? Maybe a Javascript solution? Maybe something similar to what this plugin does to the character bio field (although its limitation is based on amount of characters).

The reason of this is that I run a multiple author platform where users often make the mistake to exceed the word limit that is being printed in the list of posts.

share|improve this question
    
Check here, here and here. And reminding that it is ok to answer your own Q :) –  brasofilo Nov 10 '12 at 21:41

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
Can you please provide me more information about exactly how I attach it to the excerpt input. I will admit that I am a bit confused about how to go about this. Thank you for the respond regardless. –  Amanda Duke Nov 11 '12 at 20:12
    
@Amanda Duke example code in answer already does that ($('#excerpt')... part). –  Rarst Nov 12 '12 at 11:23
    
Yes I found that, I was referring to how I add this example code to my site? Add it to functions.php? –  Amanda Duke Nov 12 '12 at 16:22
    
@Amanda Duke this is coded like a basic plugin and the only think that will differ depending on where you put it is URL to script file. "Where code goes" is hard question to answer because there are umpteenth places to put it and you are supposed to decide that, depending on your needs and setup you have. –  Rarst Nov 12 '12 at 19:06
    
Sorry about the late reply, my account was temp suspended. Back to the subject, yes I fully understand what you mean about how to implement the code, this was not an issue. I was asking the question as I tried it already as a plugin (added full path to the js file) and nothing happened... therefore I suspected that the code may need to be added somewhere else. Nonetheless, have you tested this code? Maybe there is a typo somewhere that is preventing it from functioning? Thanks for the reply. –  Amanda Duke Nov 21 '12 at 14:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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