Support

Account

Home Forums ACF PRO Add/extend custom field settings Reply To: Add/extend custom field settings

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