Skip to:
Content
Pages
Categories
Search
Top
Bottom

Limiting text length of activity update posts

  • Avatar of xrun
    xrun
    Member

    @xrun

    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.

Viewing 22 replies - 1 through 22 (of 22 total)
  • Avatar of Nm
    nm
    Participant

    @nahummadrid

    @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.

    Avatar of xrun
    xrun
    Member

    @xrun

    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.

    Avatar of Nm
    nm
    Participant

    @nahummadrid

    @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.

    Avatar of xrun
    xrun
    Member

    @xrun

    @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. :)

    Avatar of justbishop
    justbishop
    Member

    @justbishop

    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.

    Avatar of @modemlooper
    @modemlooper
    Moderator

    @modemlooper

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

    If you want to show character count put `left: 100` next to textarea html

    @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:

    `
    //<![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;
    }
    }
    //]]>
    `

    `@ `

    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.

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

    Avatar of justbishop
    justbishop
    Member

    @justbishop

    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*

    Avatar of @modemlooper
    @modemlooper
    Moderator

    @modemlooper

    in your header.php file put it between “ 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′);
    });`

    Avatar of Nm
    nm
    Participant

    @nahummadrid

    @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.

    Avatar of justbishop
    justbishop
    Member

    @justbishop

    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

    @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.

    Avatar of Paul Gibbs
    Paul Gibbs
    Keymaster

    @djpaul

    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.

    Avatar of @modemlooper
    @modemlooper
    Moderator

    @modemlooper

    Paul, I pretty much posted the code on here. ;)

    Who wants this as an option? If it’s a majority i’ll create a patch. Though, I think if it is wanted there should be an admin option for on or off as well as how many characters

    Avatar of @mercime
    @mercime
    Keymaster

    @mercime

    @modemlooper, that’ll be a cool option +1

    Avatar of Paul Gibbs
    Paul Gibbs
    Keymaster

    @djpaul

    Decisions not options.

    Yes to an option with admin settings, which sets the limit in php, with a js counter. Not everyone is going to want this, and it’s critical that the site dev has full control over character limit, not to mention that different spoken languages can have quite different character counts for similar semantics.

    @modemlooper – I’ve gone back to your script. For some reason it was running super slow for me the other day when testing, but then I found that @nahummadrid ‘s method breaks the form submit when javascript is disabled, so I’ve gone back to it, and it’s super snappy. Pilot error on my part, clearly.

    Actually the nahummadrid method issue could be regarded as a feature in a sense, since it would prevent circumnavigation of the script limit by disabling js in the browser ;) The problem with that script without js appears to be the (depricated) form name, which has been specified with a hyphen and not camelCase, and doesn’t work with the inline onkeyup/onkeydown call. I’m not one for inline scripting anyway, so modemlooper’s is the one for me now.

    @djpaul – I’ve had problems logging into trac, and am fried for time, but we’re launching at the end of the month, and then I’ll have the time to engage with trac properly and post the bugs that have cropped up during the site build and testing. There are a few (eg. the multiple nested tags bug when any buddypress user content is edited – just have a look at the source code of this comment!). The fact that the whats-new form only works properly once between page refreshes is a bummer… On subsequent postings, the submit button has to be clicked 5 – 15 times before it fires (confirmed with fresh vanilla install in firefox, camino, safari and opera).

    Adendum – going back to the first method (nahummadrid’s). The problem with modemlooper’s method (as things stand with the whats-new bug) is that once an update is posted, if the user then wants to reply to an update before refreshing the page or navigating away, the comment reply form character limit is temporarily broken – the reply counter no longer works, and the reply limit doesn’t work either.

    Agh! This really should NOT be this hard. This has to be due to the plethora of identical nonce ids, no?

    I’ve further pinned down the whats-new bug. It can be more accurately described thus:

    “You cannot successfully post two or more sequential updates from the whats-new form”

    You can post an update, a reply, an update, a reply etc. no problem, but sequential updates require up to 15 clicks on the submit button.

    Avatar of enderpal444
    enderpal444
    Participant

    @enderpal444

    @modemlooper

    I’m dont get what I did wrong. I took the Jquery code from pastebin…put it between script tags…placed it in the header.php…and nothing happened. I can still post over 100 characters. What did I do wrong?

    Avatar of Reacton
    Reacton
    Participant

    @reacton

    @modemlooper Sorry if this upsets any parties but my Java script lecturer from last year was absolutely terrible and as such we learnt basically nothing about java script.. I do know my way around wordpress and the files in the backend however so could you please tell me how to implement this javascript code in detail?

    Any help much appriciated,

    Lloyd.

    Avatar of Reacton
    Reacton
    Participant

    @reacton

    Thanks for the help.. but found a plugin which does the job anyway.

Viewing 22 replies - 1 through 22 (of 22 total)

The topic ‘Limiting text length of activity update posts’ is closed to new replies.