Home › Forums › Feature Requests › 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;
}
The topic ‘Set max length for editor’ is closed to new replies.
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.