Home Forums Bug Reports acf/prepare_field incomplete?


acf/prepare_field incomplete?

  • I’m testing using this code:

    function my_acf_prepare_field( $field ) {
      $field['class'] .= 'form-control';
      $field['wrapper']['class'] .= 'wrapper-form-control';
      return $field;
    add_filter('acf/prepare_field', 'my_acf_prepare_field');

    On a date picker field, it doesn’t add either of those classes to the html output.

    Does someone have the definitive word on where and when classes are added, and when they’re not?!

    Is this a bug or is there a reason why the input of the date picker doesn’t get the class applied?

    And what of all the other field types?

  • I’m looking at ACF Pro > includes > fields > class-acf-field-date_picker.php:

    // elements
    		$div = array(
    			'class'					=> 'acf-date-picker acf-input-wrap',
    			'data-date_format'		=> acf_convert_date_to_js($field['display_format']),
    			'data-first_day'		=> $field['first_day'],
    		$hidden_input = array(
    			'id'					=> $field['id'],
    			'name'					=> $field['name'],
    			'value'					=> $hidden_value,
    		$text_input = array(
    			'class' 				=> 'input',
    			'value'					=> $display_value,

    Why can we not change the 3rd last line there to:

    			'class' 				=> $field['class'] . ' input',


    And do this consistently for all other field types?

    For me, this is about applying Bootstrap 4 styles to the correct field elements for front-end forms. I can’t, because I can’t get to the input element to add a class, unless I make ACF (Pro) consistent in this way…

    Is this a missing bit of code, or deliberate??


  • The only thing that ACF gives you the ability to add is a wrapper class on the field container

    <div clas"acf-field acf-field-date-picker ... my-custom-class"...

    This is where the value of $field['wrapper']['class'] will go.

    There has never been any ability to add or alter classes on any of the nested elements of a field, so I’d say this is by design.

  • I think this needs some more thought, John. Honestly.

    A basic ACF text field DOES get the $field[‘class’] attribute applied to its class.

    But the input of a datepicker field is ignored (as I show above).

    Can we not, at least, aim for simple consistency?

    $field[‘class’] should always be available, and should always be applied to the visible element, so that things like Bootstrap classes can be applied easily.

    To my mind this is sensible, and a fairly trivial update to the code. It’s very little work to do it, and a big win for consistency and usefulness.


  • What you have done is found something that is an internal ACF thing $field[‘class’]. Yes, you are correct, if you add something to this during prepare field it will be added to a basic fields class attribute. It may work with other fields as well, I don’t know.

    It does not work on the date picker field because that value is not used. A date picker field is a jquery date picker and it’s made up of multiple fields and ACF applies different classes to each field that’s needed and the jquery date picker code takes if from there.

    I think this is something that was not intended for public use, I would not use it in this manner because it is not consistently used.

    As far as updating the code, you’d need to take that up with the developer I am just another user trying to help out.

  • No advance on that issue?
    i tried this

        add_filter('acf/prepare_field/type=tab', 'sagive_validate_extra_tabclass', 10);
        function sagive_validate_extra_tabclass( $field ){
            if( !empty($field['tabclass']) ) {
                $field['class']             = 'some-class';
                $field['wrapper']['class']  = 'some-class';
            return $field;

    Doesnt work (at least on the tab element)… which is where i need it.

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

The topic ‘acf/prepare_field incomplete?’ is closed to new replies.