Support

Account

Home Forums ACF PRO acf/load_field add data attribute to field

Helping

acf/load_field add data attribute to field

  • Hi all,

    So I am trying to add a data attribute field to a specific textarea. I have managed to get the filter acf/load_field running and add data to the field object.

    
    public function addDataAttributeToAcfTextarea($field) {
            // Add custom class
            $field['class'] .= ' MyCustomClass';
    
            // Add 'data-any-type' attribute
            $field['data-code-type'] = 'whatever';
    
            // echo '<pre>';
            // print_r($field);
            // echo '</pre>';
    
            return $field;
        }
    

    But the result:

    
    <textarea id="acf-field_445545544" class="MyCustomClass" name="acf[acf-field_445545544]" rows="35">Some content</textarea>
    

    Desired the result:

    
    <textarea id="acf-field_445545544" class="MyCustomClass" name="acf[acf-field_445545544]" rows="35" data-code-type="whatever">Some content</textarea>
    

    Perhaps I am missing something?

    Greetz,
    Walter

  • You cannot add arbitrary attributes to an ACF fields that will be added when the field is rendered. Any attribute not already present in the $field array is simply ignored.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.