Support

Account

Home Forums ACF PRO Add/extend custom field settings

Solving

Add/extend custom field settings

  • Hi,
    I want to extend existing ACF wysiwyg field by adding a custom setting which will be “max word count”. I know how to add appropriate field validation filter, so the only problem is extending existing WYSIWYG field settings by adding a new input for words count number.

    Is it possible with some ACF filter/action? or do I have to create my own custom ACF field from scratch?

  • This is an add on that includes a character counter for wysywig fields, which was forked from a character counter for text fields. The best advice I can give is to look at this and adapt it do count words instead of characters. https://github.com/puntonero/acf-input-counter

  • I see there is no way of customizing existing fields settings, so I made up my own solution which is based on the instructions content. Here is the code (in case anyone is interested), what it does is limiting a maximum number of words in WYSIWYG field:

    
    // Check  if number of words in WYSIWYG is less than max number
    // provided inside field instructions. Supported instructions format is (see the regex):
    // Max. 300 words
    // max 300 words
    // max.300 words
    add_filter('acf/validate_value/type=wysiwyg', function ( $valid, $value, $field, $input ){
    	if( !$valid ) {
    		return $valid;
    	}
    	
      $matches = [];
      preg_match('/max[^\d]*(\d+) word/i', $field['instructions'], $matches);
    
      if ( empty( $matches ) || empty( $matches[1] ) ) {
        return $valid;
      }
    
      $max_words = (int) $matches[1];
    
      // check words number
      $words_number = str_word_count( strip_tags( $value ) );
      if ( $words_number > $max_words ) {
        $valid = "Text is too long ( ${words_number} words ). Max number of words is ${max_words}.";
      }
    	
    	// return
    	return $valid;
    }, 10, 4);
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Add/extend custom field settings’ is closed to new replies.