Support

Account

Home Forums Gutenberg acf_add_local_field_group: fields dont appear in Gutenberg block

Solving

acf_add_local_field_group: fields dont appear in Gutenberg block

  • hi!
    I want to build a custom plugin that will both

    – register new ACF fields (via acf_add_local_field_group() )
    – register new Gutenblocks (via acf_register_block() )

    The gutenberg block does render oke. (i see the header line)
    But in “edit” mode for some reason the new fields dont appear in the new registered blocks.

    Is it even possible? or i missed something here?

    Some testcode here:

    
    
    if ( function_exists( 'acf_add_local_field_group' ) ):
    
        add_action( 'acf/init', 'my_acf_add_local_field_groups' );
        function my_acf_add_local_field_groups() {
    
            acf_add_local_field_group( array(
                'key'                   => 'group_1',
                'title'                 => 'Block: Podcast',
                'fields'                => array(
                    array(
                        'key'               => 'field_1',
                        'label'             => 'Testblock field',
                        'name'              => 'testblock_field',
                        'type'              => 'text',
                        'instructions'      => '',
                        'required'          => 0,
                        'conditional_logic' => 0,
                        'wrapper'           => array(
                            'width' => '',
                            'class' => '',
                            'id'    => '',
                        ),
                        'default_value'     => '',
                        'placeholder'       => '',
                    ),
                ),
                'location'              => array(
                    array(
                        array(
                            'param'    => 'block',
                            'operator' => '==',
                            'value'    => 'acf/test_block',
                        ),
                    ),
                ),
                'menu_order'            => 0,
                'position'              => 'normal',
                'style'                 => 'default',
                'label_placement'       => 'top',
                'instruction_placement' => 'label',
                'hide_on_screen'        => '',
                'active'                => true,
                'description'           => '',
                'metabox_title'         => '',
            ) );
        }
    
        add_action( 'acf/init', 'my_register_blocks' );
        function my_register_blocks() {
    
            // check function exists
            if ( function_exists( 'acf_register_block' ) ) {
                // register a testimonial block
                acf_register_block( array(
                    'name'            => 'test_block',
                    'title'           => __( 'ACF TEST block plugin' ),
                    'description'     => __( 'A test block.' ),
                    'render_callback' => 'my_acf_block_render_callback',
                    'category'        => 'formatting',
                    'icon'            => 'admin-comments',
                    'mode'            => 'auto',
                    'keywords'        => []
                ) );
            }
        }
    
    endif;
    
    function my_acf_block_render_callback( $block, $content = '', $is_preview = false ) {
        /* print_r($block);
        print_r($content); */
        $headlinegt = get_field( 'podcast_link' );
        ?>
        <section class="tp-text-onecolumn-gutenberg">
            <div class="container">
                <div class="row">
                    <div class="col-12">
                        <div class="headline-container">
                            <h2 class="headline">Testing header></h2>
                            <h2 class="headline"><?php echo $headlinegt; ?></h2>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    
        <?php
    }
    
  • Same issue here! I can register fields via ACF plugin UI, but not via acf_add_local_field_group function.

  • Doesnt work for me also.

  • I found a bug and fix it.
    YOu are not able to use underscore in block name when you set locations for acf_add_local_field_group

    Instead just replace all underscore with simple score and it will work.

    acf_register_block( [
    ‘name’ => ‘test_any’,
    ‘title’ => ‘Test’,
    ‘render_callback’ => function() {
    ?>
    <h2>123</h2>
    <?php
    }
    ] );

    acf_add_local_field_group(array(
    ‘key’ => ‘block_test’,
    ‘title’ => ‘Block Test’,
    ‘active’ => 1,
    ‘fields’ => array (
    array (
    ‘key’ => ‘field_1’,
    ‘label’ => ‘Sub Title’,
    ‘name’ => ‘sub_title’,
    ‘type’ => ‘text’,
    )
    ),
    ‘location’ => array (
    array (
    array (
    ‘param’ => ‘block’,
    ‘operator’ => ‘==’,
    ‘value’ => ‘acf/test-any’,
    ),
    ),
    ),
    ));

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

You must be logged in to reply to this topic.