Created
July 3, 2014 16:13
-
-
Save michaeloeser/0019bf9784f89cc82e85 to your computer and use it in GitHub Desktop.
Limit WordPress Comments to 1000 Characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// limit the comments to 1.000 characters | |
jQuery(function($) { | |
// configure | |
var comment_input = $( '#commentform textarea#comment' ); | |
var submit_button = $( '#commentform #submit' ); | |
var comment_limit_chars = 1000; | |
// stop editing here | |
// display how many characters are left | |
$( '<div class="comment_limit_info"><span>' + comment_limit_chars + '</span></div>' ).insertAfter( comment_input ); | |
comment_input.bind( 'keyup', function() { | |
// calculate characters left | |
var comment_length = $(this).val().length; | |
var chars_left = comment_limit_chars - comment_length; | |
// display characters left | |
$( '.comment_limit_info span' ).html( chars_left ); | |
// disable submit button if too many chars were used and add css class "commentInputError" to the characters left ouput "comment_limit_info" | |
if( chars_left < 0 ) | |
{ | |
submit_button.attr('disabled', 'disabled'); | |
$( '.comment_limit_info' ).addClass('commentInputError'); | |
} | |
else | |
{ | |
submit_button.removeAttr('disabled'); | |
$( '.comment_limit_info' ).removeClass('commentInputError'); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment