Group Mods

  • Profile picture of @mercime
  • Profile picture of r-a-y
  • Profile picture of Hugo

Support: Creating & Extending

Existing and new plugins/components and themes.

Limiting text length of activity update posts (21 posts)

Started 1 year, 10 months ago by: xrun

  • Profile picture of xrun xrun said 1 year, 10 months ago:

    Hello.
    I’m looking for a way to limit the length of text users can write in their update posts, for example 250 characters max. I’ve been tweaking some of the template files for other things and I’m thinking maybe I could tweak post-form.php to achieve this. I’m just not sure about the syntax. I would be greatful if someone could point me in the right direction.

  • Profile picture of Nahum Nahum said 1 year, 10 months ago:

    @xrun try this from the post i made about it and change the number to 250. if there’s any suggestions on how to make better anyone, feel free. I just had started using this a very long time ago and its worked for the most part ever since. The only thing is that sometimes a member can still copy and paste text longer than the limit and it’ll post, don’t know why that is.

  • Profile picture of xrun xrun said 1 year, 10 months ago:

    Yepp, that works like a charm. I’d like the number displayed like a label though, not in a text box. Makes it a bit more consistent with the theme.

  • Profile picture of Nahum Nahum said 1 year, 10 months ago:

    @xrun Cool. Either way…you can style the text box just the same. do you have anywhere to see in action? How about that copy and paste scenario? Glad it worked.

  • Profile picture of xrun xrun said 1 year, 10 months ago:

    @nahummadrid Sorry for the late reply.
    The copy/paste thing seems not to be an issue, it still limits the length of the entry correctly. I’ve tried styling it a bit, it’s a work in progress. The site is not launched just yet and I’m working under an NDA, but I’ll let you know. :)

  • Profile picture of justbishop justbishop said 11 months, 2 weeks ago:

    Tried to implement this, but I’m wondering if the code was mangled a bit on the post that @nahummadrid linked to. It doesn’t seem to want to work for me.

  • Profile picture of modemlooper modemlooper said 11 months, 2 weeks ago:

    Here is a jquery version. http://pastebin.com/PP1BPSQT

    If you want to show character count put left: <span>100</span> next to textarea html

  • Profile picture of aljuk aljuk said 11 months, 2 weeks ago:

    @nahummadrid – nice solution. It’s concise, and responsive (VERY important). Every other solution I’ve tested slows down the text input to a crawl, which isn’t acceptable for a public site and will really annoy anyone who types with all their fingers. I haven’t encountered the issue to which you alluded re. users breaking the limit with copy/paste. It’s working flawlessly. Thanks a bunch for it.

    @justbishop – yes, the code display has been mangled by wp texturise, and contains a cpl small errors. Here it is clean:

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

    <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 if ( isset( $_GET['r'] ) ) : ?>@<?php echo esc_attr( $_GET['r'] ) ?> <?php endif; ?></textarea>

    <input readonly="readonly" type="text" name="countdown" size="3" value="148" id="whateveryouwant" />

    Incidentally, I think it’s best to place the script itself in entry.php, not post-form.php (assuming you’re also setting a character limit on replies). The reason for this is that it should only be loaded once, and if you place it in post-form.php it won’t be available on an individual activity page (ie. the page you get when you click the “View” link), but if you place it in entry.php it will be loaded when viewing the activity stream, and also when viewig the individual activity.

  • Profile picture of aljuk aljuk said 11 months, 2 weeks ago:

    One slight niggle. How to reset the counter upon submit?

  • Profile picture of justbishop justbishop said 11 months, 2 weeks ago:

    Thanks guys!

    @modemlooper: at the risk of seeming like a total idiot, does the code in your pastebin link go between php open/close tags? To be honest, I’m not quite sure at all what to do with it *blush*

  • Profile picture of modemlooper modemlooper said 11 months, 2 weeks ago:

    in your header.php file put it between <script></script> tags or you can have your own .js file to place custom javascript

    @aljuck try something like this:

    jQuery('#aw-whats-new-submit').click(function() {
     jQuery('textarea').next().text('100');
    });
  • Profile picture of Nahum Nahum said 11 months, 2 weeks ago:

    @aljuk thanks for the tip. that was just something i dragged together and it has worked for me. i’m no pro is for sure but what a pro once told me, “if it works!, it works!” hehe. I thought the copy and paste issue was an issue but folks are saying it hasn’t been so right on.

    @justbishop sorry i had my code highlighter plugin deactivated on that post…i’ve fixed it now.

  • Profile picture of justbishop justbishop said 11 months, 2 weeks ago:

    Thanks again gentlemen (I’m ASSuming you’re all men, if not, I apologise!) I tried both methods, but ended up gpong with the one laid out by @aljuk :D

  • Profile picture of aljuk aljuk said 11 months, 2 weeks ago:

    @nahummadrid @justbishop @modemlooper

    modemlooper – you’re totally right, the script should be called from the header or an external js. Placing it in post-form.php only calls it for the whats-new form, and placing it in entry.php calls it repeatedly for every comment reply, leading to multiple instances.

    I adapted your reset script suggestion for use with nahummadrid’s technique of using a read-only text input field to display the count:

    jQuery('#aw-whats-new-submit,.ac_form_submit').click(function() {
    		jQuery('.charlimitinfo').val('240');
    	});

    .charlimitinfo is my count field
    .ac_form_submit is a class I’ve attached to the submit input for activity comments (in entry.php)
    240 is my chosen text limit

    The only issue I’ve encountered is that if you post something via the whats-new form and then post a comment reply without refreshing the page first, the reset for the character limit for the comment reply doesn’t fire. I guess this is related to the bug in Buddypress that prevents the whats-new form functioning correctly after an update’s been made and the page hasn’t been refreshed – perhaps due to the use of multiple identical ids and nonce field ids in the activity stream? I’m no expert, just a guess.

  • Profile picture of Paul Gibbs Paul Gibbs said 11 months, 2 weeks ago:

    If someone wants to make a trac ticket and a patch to discuss limiting activity stream replies to a length similar of twitter, go for it.