Support

Account

Home Forums Feature Requests Set max length for editor Reply To: Set max length for editor

  • 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;
        
        
    }