19. Registrierung von Feldgruppen mit PHP | Field Forge - Benutzerdefinierte Felder, gebaut für Geschwindigkeit
Herunterladen Anmelden

19. Registrierung von Feldgruppen mit PHP

Erstellen Sie Feldgruppen programmgesteuert anstelle über die Admin-UI. Dies ist nützlich für Plugins, die ihre eigenen Feldgruppen mitliefern, oder für Teams, die eine codebasierte Konfiguration bevorzugen.

Grundlegende Registrierung

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' ] ],
        ],
    ] );
} );

Idempotente Registrierung

Verhindern Sie doppelte Gruppen bei jedem Seitenaufruf:

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' => [ /* ... */ ],
        ] );
    }
} );

Registrierung mit Wiederholern und flexiblem Inhalt

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 KI-Assistent Online

Hallo! Ich bin der Field Forge KI-Assistent. Fragen Sie mich alles über das Plugin — Einrichtung, Funktionen, Fehlerbehebung oder Entwicklung.

Gerade eben
Unterstützt von Forge KI · Dokumentation durchsuchen