Support

Account

Home Forums Feature Requests Set max length for editor

Solving

Set max length for editor

  • Hi,
    I would like to limit my field in wordpress dashboard. Something like max-length=”100″ for max 100 chars. Is it possible?

  • Hi @rshmiraf

    Could you please tell me which field type is it? I believe the text and text area already have the option to do it. For the other field, I think you can use the acf/validate_value filter instead. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/acf-validate_value/.

    I hope this helps ๐Ÿ™‚

  • Hi James,
    I tried this but without result. This field is wysiwyg editor end text field. Could you share me example with code / option which limit word character for example 40 chars.

  • Hi @rshmiraf

    For WYSIWYG field, I believe you can do it like this:

    add_filter('acf/validate_value/name=wysiwyg_field_name', 'my_acf_validate_value', 10, 4);
    
    function my_acf_validate_value( $valid, $value, $field, $input ){
        
        // bail early if value is already invalid
        if( !$valid ) {
            
            return $valid;
            
        }
        
        if( strlen($value) > 40 ) {
            
            $valid = 'You can\'t enter more that 40 chars';
            
        }
        
        
        // return
        return $valid;
        
        
    }

    If still no joy, could you please share the code you have?

    Thanks ๐Ÿ™‚

  • Hi,

    This code works, but it counts the number of total HTML characters in the field, not the rendered number of characters (what the user sees in the visual editor).

    For example, if you type the text ยซ Hello World ยป, it will count 11 characters. But if you put this same text in bold, it will count 28 characters, because the content of the field is actually : <strong>Hello World</strong>.

    So a better solution would be to strip the tags from the value variable before counting the number of characters, like so :

    add_filter('acf/validate_value/name=wysiwyg_field_name', 'my_acf_validate_value', 10, 4);
    
    function my_acf_validate_value( $valid, $value, $field, $input ){
        
        // bail early if value is already invalid
        if( !$valid ) {
            
            return $valid;
            
        }
        
        if( strlen(strip_tags($value)) > 40 ) {
            
            $valid = 'You can\'t enter more that 40 chars';
            
        }
        
        
        // return
        return $valid;
        
        
    }
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Set max length for editor’ is closed to new replies.