Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to force size limits on user posts? (Not just for display) WordPress

  • SOLVED

I have a wordpress site where many users can sign up to submit their own posts. Is there a way of limiting the size of their posts (to, for example, 500 characters) ? I can find existing plug-ins which will limit the <em>displayed</em> excerpt size for a post on a front page or wherever, but that is not what I want. I want to limit the <strong>actual</strong> amount of text users can submit in their post. I have found a plug-in that will do this for post titles, and another for post comments,<strong> but what I really want is to limit the actual content of the user post itself.
</strong>
Thanks for any suggestions!

---
Update: thanks to the people who have already replied - this is my first time using this site and I am impressed already!
However, I wanted to update my question to emphasize that I don't have much coding experience at all, whether it's jquery or php or whatever.
I am happy to go and type/paste in things to try out - but I'll need instructions about how exactly to go about it in the Wordpress interface itself (or something just as easy). So if you have a straightforward solution for a beginner, that would be the best. Thanks!

Answers (3)

2012-10-31

Arnav Joy answers:

you can do it using jquery

suppose your textarea has id "text" as

<textarea id="text"></textarea>

so you can use following jquery to place limit

<script>

var characters= 100;
jQuery(document).ready(function($){
$("#text").keyup(function(){
if($(this).val().length > characters){
$(this).val($(this).val().substr(0, characters));
}
})

</script>


Arnav Joy comments:

see this link
http://www.yourinspirationweb.com/en/jquery-tips-tricks-how-to-limit-characters-inside-a-textarea/

2012-10-31

Francisco Javier Carazo Gil answers:

If you want to do it with PHP instead of JS you will have to define a function to the action save_post (http://codex.wordpress.org/Plugin_API/Action_Reference/save_post) and then in this function check the actual size.


Francisco Javier Carazo Gil comments:

It size is too long, delete the post and give a message to the author (or even make a substring and save only this).

2012-10-31

Dbranes answers:

Hi,

If you mean how to limit the characters in the WordPress Editor, then this might be helpful

http://konstruktors.com/blog/wordpress/3685-limit-number-words-characters-in-wordpress-editor/

just let me know if you need help implementing this code.


Dbranes comments:

I modified the following:

1) code from http://konstruktors.com/blog/wordpress/3685-limit-number-words-characters-in-wordpress-editor/
2) code example from Arnav Joy (above)

into a code that forces a limit on the number of characters in the WordPress Editor (both visual and html)


The code in 1) only handles the visual editor and does not prevent writing beond the limit, so I changed that.

I use the code in 2) to force the limit on the html editor.


You can add this into your functions.php file in your current theme directory:

add_action( 'admin_print_footer_scripts', 'check_textarea_length' );

function check_textarea_length() {
?>
<script type="text/javascript">

var visual_editor_char_limit = 50;
var html_editor_char_limit = 50;
var char_limit_warning="Reduce word count!";

// jQuery ready fires too early, use window.onload instead
window.onload = function () {
// are we using visual editor?
var visual = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden()?true:false;
if(visual){
jQuery('.mceStatusbar').append('<span class="word-count-message">'+ char_limit_warning +'</span>');

tinyMCE.activeEditor.onKeyDown.add( function(ed,e) {
// Strip HTML tags, WordPress shortcodes and white space
editor_content = this.getContent().replace(/(<[a-zA-Z\/][^<>]*>|\[([^\]]+)\])|(\s+)/ig,'');

// debug:
//console.log("tinymce editor current #chars: "+editor_content.length);

if ( editor_content.length > visual_editor_char_limit ) {

jQuery('#content_tbl').addClass('toomanychars');

// stop any more character input
e.returnValue = false;
e.cancelBubble = true;

// debug:
//console.log(" stop writing! ");

return false;

} else {
jQuery('#content_tbl').removeClass('toomanychars');
}
});
}

// do we have html editor?
if(jQuery("#content").length){
jQuery("#content").keyup(function(){
// debug:
// console.log("html editor current #chars : " + jQuery(this).val().length );
if(jQuery(this).val().length > html_editor_char_limit){
jQuery(this).val(jQuery(this).val().substr(0, html_editor_char_limit));
alert(char_limit_warning);
}
});
}
}

</script>

<style type="text/css">
.wp_themeSkin .word-count-message { font-size:1.1em; display:none; float:right; color:#fff; font-weight:bold; margin-top:2px; }
.wp_themeSkin .toomanychars .mceStatusbar { background:red; }
.wp_themeSkin .toomanychars .word-count-message { display:block; }
</style>
<?php
}


You can uncomment the console.log if you want to debug it.

Let me know if this gives you any problems - this seems to work on my Wordpress (3.4.2) install.