Home Forums General Issues Defining field wrapper ID


Defining field wrapper ID

  • Hi.

    As far as any of you know, is there a way to define a fields wrapper ID using PHP, instead of having to manually input it in the field group page?


  • This could be done using an acf/load_field filter. All of the fields properties are editable using this filter.

    If you want to see what all of the properties of a field are you can add this to your function during testing

    echo '<pre>'; print_r($field); echo '</pre>';


  • Thanks for the tip John. I’ll investigate load_filter and see if I can get it to work.

  • I gave that a bash John, and I think I am almost there with it.

    One last this I would like to attempt is to sequentially name the IDs im defining using load_field.

    For example, if I have several repeater fields, to be able to define their wrapper IDs as “box1”, “box2”, “box3” etc. They all share the same field ID so im finding it difficult targeting each one specifically. Any ideas on how to pass this extra value via the filter?

    Thanks again. You’ve been a great help.

  • Are you talking about giving each of the repeater’s sub fields a unique CSS ID?

  • Yes. For example, say I have a repeater containing a text field, and on the edit page I add 5 repeater rows. I want to target the text fields and give them each a unique wrapper ID.

    Currently my code looks like this:-

    function my_acf_load_field( $field )
        $field['wrapper'] = array(
            'id' => 'unique_id'
        return $field;
    	add_filter('acf/load_field/key=field_123', 'my_acf_load_field');

    With field_123 being the text field. But this will give all 5 text fields the same ID: ‘unique_id’. What I would like is for each text field to have a sequential ID. for example “unique_id_1′, ‘unique_id_2’… etc.

    Any ideas?

  • Sorry, did not see your follow up question. For some reason I stopped getting notification, not sure why.

    Anyway, giving each field in a repeater a unique value is somewhat harder. You need to have a way to keep track of the last value you assigned and increment it. I think something like this could work.

    function my_acf_load_field($field) {
        static $count = 0;
        $field['wrapper'] = array(
            'id' => 'unique_'.$count;
        return $field;
    	add_filter('acf/load_field/key=field_123', 'my_acf_load_field');
  • Thanks James. Nice idea using static variables. Unfortunately, when using your example, $count is always 1. It seems to be getting reset to 0 even though its defined as static. As far as I can see it should work. Any clue as to why?

  • Well,

    I have been doing some testing. Load field is only called once which explains why the value is not changing for you. The reason I suggested it is that at one time load field was called for each instance, at least I think I remember it that way.

    However, I did notice while testing that if I set an ID for a sub field of a repeater then that ID is used multiple times. I think that is a bug, the ID value should be appended with the row by ACF, at least I think so. I have submitted an bug report for this.

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

The topic ‘Defining field wrapper ID’ is closed to new replies.