Skip to:
Content
Pages
Categories
Search
Top
Bottom

Limit maximum number of characters in Topic Content TextArea


  • tekgazet
    Participant

    @tekgazet

    I want to limit the number of characters in the Topic Content Text Area in bbPress to a maximum limit (of say 500 characters). How can I do it?

    I tried to do it by using the maxlength attribute of textarea (bbp_topic_content) in the stylesheet file, but this attribute is not working (may perhaps be because this attribute was included in HTML5 version only). However, if I use this in Developer Tools in Chrome while inspecting the text area for Topic Content, it works fine; but that is only for testing and I am not able to make a permanent change by editing the source file or through any plugin.

    Other two methods could be javascript to check on local machine the number of characters being entered, but I understand that this can easily be cheated by disabling javascript. Moreover, I am not fully sure how to do it.

    Third method could be to use for action or filter hook. Can you please suggest me which hook to be used and sample code, if any?

    Thanks in advance.

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

  • tekgazet
    Participant

    @tekgazet

    Never mind. After some trial and error, I have been able to find out the relevant filter hook and achieved the goal. Thanks.


    Robin W
    Moderator

    @robin-w

    would be great if you posted the answer to help others


    tekgazet
    Participant

    @tekgazet

    Sure. I used the bbp_new_topic_pre_content Filter Hook and the code I used is as under:

     //code to limit number of characters in topic contents to 500 characters
     
    function tm_limit_topic_content( $topic_content )
    {
        /* Check for number of characters in topic contents  */
        if (strlen($topic_content) > 500) {
    		$revised_content = substr($topic_content, 0, 500);
    		}	
    	else {
    		$revised_content = $topic_content ;
    	}		
     
        return $revised_content ;
    }
     
    add_filter( 'bbp_new_topic_pre_content', 'tm_limit_topic_content' );

    Robin W
    Moderator

    @robin-w

    great – thanks for posting that – really appreciated !

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Skip to toolbar