Top menu

Add a Character Limit to Buddypress Activity Post Form

Here’s how I added length limiting to my post box in Buddypress.

Open the activity/post-form.php and add this near top. I put it under the avatar, don’t remember right now if there was a reason for that.

[php]
<script type="text/javascript">
<!– Begin
function limitText(limitField, limitCount, limitNum) {
if (limitField.value.length > limitNum) {
limitField.value = limitField.value.substring(0, limitNum);
} else {
limitCount.value = limitNum – limitField.value.length;
}
}
// End –>
</script>
[/php]

Add this next clip right afterwards or depending where you want it to display the number counter, you can move around as you please with #numbCount or whatever you’d like to call it in your style sheet.  Change the value number according to how many characters you want to allow, i’ve place 148 (sounded like a good number)

[php]
<input readonly type="text" name="countdown" size="3" value="148" id="numbCount">
[/php]

Next replace the following

[php]
<textarea name="whats-new" id="whats-new" cols="50" rows="10"><?php if ( isset( $_GET[‘r’] ) ) : ?>@<?php echo esc_attr( $_GET[‘r’] ) ?> <?php endif; ?></textarea>
[/php]

with the following, remember to include the value after where it says countdown.

[php]

<textarea id="whats-new" cols="50" rows="10" name="limitedtextarea" onKeyDown= "limitText(this.form.limitedtextarea,this.form.countdown,148);"             onKeyUp="limitText(this.form.limitedtextarea,this.form.countdown,148);"" [/php]

That’s how i got it to work. Any suggestions on how to optimize or make it better, leave me a note. Like for instance, I think users can copy and paste text longer than the limit and it’ll accept it…I haven’t looked into it as it doesn’t happen that often, but if you know please share.

, ,

No comments yet.

Leave a Reply