19. Registering Field Groups with PHP | Field Forge - Custom Fields, Built for Speed
Download Log in

19. Registering Field Groups with PHP

Developer Guide

Create field groups programmatically rather than through the admin UI. This is useful for plugins that ship their own field groups, or for teams that prefer code-based configuration.

Basic Registration

php
add_action( 'init', function() {
    if ( ! class_exists( 'FIELDFORGE_Field_Groups' ) ) {
        return;
    }

    FIELDFORGE_Field_Groups::instance()->create( [
        'title'  => 'Product Fields',
        'fields' => [
            [
                'key'      => 'field_prod_price',
                'label'    => 'Price',
                'name'     => 'price',
                'type'     => 'number',
                'required' => true,
                'min'      => '0',
                'step'     => '0.01',
            ],
            [
                'key'   => 'field_prod_sku',
                'label' => 'SKU',
                'name'  => 'sku',
                'type'  => 'text',
            ],
            [
                'key'           => 'field_prod_gallery',
                'label'         => 'Gallery',
                'name'          => 'product_gallery',
                'type'          => 'gallery',
                'return_format' => 'array',
            ],
        ],
        'location_rules' => [
            [ [ 'param' => 'post_type', 'operator' => '==', 'value' => 'product' ] ],
        ],
    ] );
} );

Idempotent Registration

Prevent duplicate groups on every page load:

php
add_action( 'init', function() {
    $groups = FIELDFORGE_Field_Groups::instance()->get_all();
    $existing = array_map( function( $g ) { return $g->title; }, $groups );

    if ( ! in_array( 'Product Fields', $existing, true ) ) {
        FIELDFORGE_Field_Groups::instance()->create( [
            'title'  => 'Product Fields',
            'fields' => [ /* ... */ ],
            'location_rules' => [ /* ... */ ],
        ] );
    }
} );

Registering with Repeaters and Flexible Content

php
FIELDFORGE_Field_Groups::instance()->create( [
    'title'  => 'Page Builder',
    'fields' => [
        [
            'key'          => 'field_pb',
            'label'        => 'Sections',
            'name'         => 'page_sections',
            'type'         => 'flexible_content',
            'button_label' => 'Add Section',
            'layouts'      => [
                [
                    'key'        => 'layout_hero',
                    'name'       => 'hero',
                    'label'      => 'Hero',
                    'sub_fields' => [
                        [ 'key' => 'field_h1', 'label' => 'Heading', 'name' => 'heading', 'type' => 'text', 'required' => true ],
                        [ 'key' => 'field_h2', 'label' => 'Image', 'name' => 'image', 'type' => 'image', 'return_format' => 'array' ],
                    ],
                ],
                [
                    'key'        => 'layout_text',
                    'name'       => 'text_block',
                    'label'      => 'Text Block',
                    'sub_fields' => [
                        [ 'key' => 'field_t1', 'label' => 'Content', 'name' => 'content', 'type' => 'wysiwyg' ],
                    ],
                ],
            ],
        ],
    ],
    'location_rules' => [
        [
            [ 'param' => 'post_type', 'operator' => '==', 'value' => 'page' ],
            [ 'param' => 'post_template', 'operator' => '==', 'value' => 'page-builder.php' ],
        ],
    ],
] );

Forge AI Assistant Online

Hi! I'm the Field Forge AI assistant. Ask me anything about the plugin — setup, features, troubleshooting, or development.

Just now
Powered by Forge AI · Browse docs