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;
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.