Register an ACF Field Group via PHP

Define a complete ACF field group programmatically using acf_add_local_field_group() for version-controlled, portable field definitions.

php configuration
|

Define a complete ACF field group programmatically using acf_add_local_field_group() for version-controlled, portable field definitions.

Register a field group with fields on the acf/init action:

<?php
add_action( 'acf/init', function () {
    acf_add_local_field_group( [
        'key'      => 'group_event_details',
        'title'    => 'Event Details',
        'fields'   => [
            [
                'key'       => 'field_event_date',
                'label'     => 'Event Date',
                'name'      => 'event_date',
                'type'      => 'date_picker',
                'required'  => 1,
                'display_format' => 'd/m/Y',
                'return_format'  => 'Ymd',
            ],
            [
                'key'       => 'field_event_location',
                'label'     => 'Location',
                'name'      => 'location',
                'type'      => 'text',
                'required'  => 1,
            ],
            [
                'key'       => 'field_event_description',
                'label'     => 'Description',
                'name'      => 'description',
                'type'      => 'wysiwyg',
                'tabs'      => 'all',
                'toolbar'   => 'full',
                'media_upload' => 1,
            ],
            [
                'key'       => 'field_event_featured',
                'label'     => 'Featured Event',
                'name'      => 'is_featured',
                'type'      => 'true_false',
                'ui'        => 1,
                'default_value' => 0,
            ],
        ],
        'location' => [
            [
                [
                    'param'    => 'post_type',
                    'operator' => '==',
                    'value'    => 'event',
                ],
            ],
        ],
        'position'          => 'normal',
        'style'             => 'default',
        'label_placement'   => 'top',
        'active'            => true,
        'show_in_rest'      => true,
    ] );
} );

Tip: To generate the PHP array for an existing field group, export it from the ACF admin under Tools > Export > Generate PHP.

Field keys (e.g. field_event_date) must be globally unique. Use a consistent prefix to avoid collisions with other plugins.

Related Snippets

Stay Updated

Get ACF tips and new extensions in your inbox

No spam. Unsubscribe anytime.