4

I want to programmatically add a tab with repeater inside but I can't seem to find a solution, I've googled all available resources but still not working.

I already tried using acf_add_local_field_group and acf_add_local_field but still no luck.

Well I can create a tab using acf_add_local_field but when I tried to add a child which in this case a repeater OR even a text field it still doesn't work.

Here's my code to create a tab and its child but the child doesn't work.

 acf_add_local_field(array(
'key' => 'field_1',
'label' => 'Sub Title',
'name' => 'sub_title',
'type' => '',
'parent' => 'field_5bd14c9349930',
'fields' => array (
    array(
            'key' => 'field_2',
            'label' => 'This is a test',
            'name' => 'my_test',
            'type' => 'text',
            )
        )
));
leonardeveloper
  • 1,813
  • 1
  • 34
  • 58

1 Answers1

-1

You should use acf_add_local_field_group to construct the whole field group.

Here's the proper code for adding a group and a custom tab with single repeater field inside:

if( function_exists('acf_add_local_field_group') ):

acf_add_local_field_group(array (
    'key' => 'group_1',
    'title' => 'My Group',
    'fields' => array (
        array (
            'key' => 'field_unique_key',
            'label' => 'First Tab', 
            'name' => '',
            'type' => 'tab',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array (
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'placement' => 'top',
            'endpoint' => 0,
        ),
        array (
            'key' => 'field_unique_key',
            'label' => 'Simple Repeater',
            'name' => 'simple_repeater',
            'type' => 'repeater',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array (
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'collapsed' => '',
            'min' => 0,
            'max' => 10,
            'layout' => 'table',
            'button_label' => 'Add row',
            'sub_fields' => array ( // Here you can add as many subfields for this repeater as you want
                array (
                    'key' => 'field_unique_key',
                    'label' => 'Link',
                    'name' => 'link',
                    'type' => 'link', // example link type
                    'instructions' => 'Link name and URL',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array (
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'return_format' => 'array',
                ),
            ),
        ),
    ),
    'location' => array (
        array (
            array (
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'post',
            ),
        ),
    ),
    'menu_order' => 0,
    'position' => 'normal',
    'style' => 'default',
    'label_placement' => 'top',
    'instruction_placement' => 'label',
    'hide_on_screen' => '',
));

endif;
Nesho Sabakov
  • 80
  • 1
  • 3