0

I am trying to make Gutenberg blocks using the beta version of ACF which provides support for it. I am able to do so with the GUI (as shown first) but cannot do so programmatically.

In the functions.php file, I start off by declaring the block I want to use as a block:

function register_acf_components() {

    if (function_exists('acf_register_block')) {

        $component = array(
            'name' => 'testimonial'
            'title' => 'Testimonial',
            'description' => 'A testimonial block'
            'render_callback' => 'acf_component_render_callback',
            'category' => 'formatting',
            'icon' => 'controls-reversed',
            'keywords' => array('testimonial')
        );

        acf_register_block($component);

}
add_action('acf/init', 'register_acf_components');

function acf_component_render_callback($component) {

    $timber = new Timber\Timber();

    ...

    $timber::render('path/to/specific/block.twig');

}

And after this if I create the field group through the ACF GUI and assign its location to block === testimonial it all works great and I can add it to pages, add content to it, and display it on a page.

However, I would like to avoid the GUI if possible and declare the field group programmatically.

This is where I stumbled upon: ACF | Register fields via PHP.

Following the advice in this article, I deleted the field group from the GUI and instead added the following to the functions.php file:

if (function_exists('acf_add_local_field_group')):

    acf_add_local_field_group(array(
        'key' => 'group_testimonial',
        'title' => 'Testimonial',
        'fields' => array(
            array(
                'key' => 'field_testimonial_header',
                'label' => 'Header',
                'name' => 'header',
                'type' => 'text',
            )
        ),
        'location' => array(array(array(
            'param' => 'block',
            'operator' => '==',
            'value' => 'acf/testimonial',
        )))
    ));

endif;

This does create the block and I can see and edit the header field in the editor but when I save it and go to view the page, there is no data.

I investigated the database hoping to find the cause of the issue and I did notice that when creating a field group via the GUI it did add a template for the field group that it did not add when doing so programmatically. I am not sure if this is the cause or if it saves the templates in memory when doing it programmatically.

Any insight into this would be greatly appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.