Word Limit for an ACF Text Area
Shows an error when a text area or WYSIWYG field goes over a set number of words, which keeps summaries and teasers short.
Opens WPIDE on
Code
// Field name and maximum words.
$wpide_acf_words_field = 'summary';
$wpide_acf_words_max = 60;
add_filter( 'acf/validate_value/name=' . $wpide_acf_words_field, function ( $valid, $value ) use ( $wpide_acf_words_max ) {
if ( true !== $valid ) {
return $valid;
}
$words = str_word_count( wp_strip_all_tags( (string) $value ) );
if ( $words > $wpide_acf_words_max ) {
return sprintf( 'Please keep this under %1$d words (currently %2$d).', $wpide_acf_words_max, $words );
}
return $valid;
}, 10, 2 );
Notes
Set the field name and limit. ACF's own character limit counts characters, not words.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.