Support

Account

Home Forums Gutenberg Programatically registering ACF field blocks in Gutenberg

Solving

Programatically registering ACF field blocks in Gutenberg

  • Hi,
    Im trying to register ACF fields programatically to use in blocks in the next WordPress update. I’m able to register the ACF fields in the php as normal which I can only see when Gutenberg is disabled, and I am able to register the ACF blocks as normal which I can see when Gutenberg is enabled, however I am unable to see the fields inside the blocks when Gutenberg is enabled.

    I have ensured the block is set up correctly because I can set fields created in the WordPress backend to it and see them, and I know the registered field is set up correctly because when Gutenberg is disabled I am able to see and fill in these fields (without location on block).

    So what Im wondering is that if there is any extra required or if i’m just missing something I’ve done entirely.

    Here is my block setup:

    if( function_exists('acf_register_block') ) {
    
      acf_register_block(array(
        'name'				=> 'textarea_block',
        'key'				=> 'textarea_block',
        'title'				=> __('textarea'),
        'description'		=> __('A custom pixel block.'),
        'render_callback'	=> 'textarea_block_render_callback',
        'category'			=> 'formatting',
        'icon'				=> 'admin-comments',
        'keywords'			=> array( 'flexible', 'quote' ),
      ));
    }

    and Here is my field setup

    register_field_group(array (
      'key' => 'textarea',
      'title' => 'Text Area',
      'fields' => array(
        array(
          'key' => 'textarea_title',
          'name' => 'textarea_title',
          'label' => 'Section Title',
          'instructions' => '',
          'type' => 'text',
        ),
        array(
          'key' => 'textarea_text',
          'name' => 'textarea_text',
          'label' => 'Text',
          'instructions' => '',
          'type' => 'wysiwyg',
        ),
      ),
      'location' => array (
        array (
          array (
            'param' => 'block',
            'operator' => '==',
            'value' => 'textarea'
          ),
        ),
      ),
    ));

    Thanks if anyone notices anything or has any ideas.

  • It seems that underscores in block name isn’t passing though.

    So, textarea_block as a block name is not working, instead use a name without underscores, something like textareablock.

    Try this:

    if( function_exists('acf_register_block') ) {
    
      acf_register_block(array(
        'name'				=> 'textareablock',
        'key'				=> 'textarea_block',
        'title'				=> __('textarea'),
        'description'		=> __('A custom pixel block.'),
        'render_callback'	=> 'textarea_block_render_callback',
        'category'			=> 'formatting',
        'mode'              => 'edit', // 'preview',
        'icon'				=> 'admin-comments',
        'keywords'			=> array( 'flexible', 'quote' ),
      ));
    }
    
    register_field_group(array (
      'key' => 'textarea',
      'title' => 'Text Area',
      'fields' => array(
        array(
          'key' => 'textarea_title',
          'name' => 'textarea_title',
          'label' => 'Section Title',
          'instructions' => '',
          'type' => 'text',
        ),
        array(
          'key' => 'textarea_text',
          'name' => 'textarea_text',
          'label' => 'Text',
          'instructions' => '',
          'type' => 'wysiwyg',
        ),
      ),
      'location' => array (
        array (
          array (
            'param' => 'block',
            'operator' => '==',
            'value' => 'acf/textareablock'
          ),
        ),
      ),
    ));

    P.S. I’ve added 'mode'=> 'edit' to start ACF block in edit more.

  • It seems like not accepting an underscore is a bug. Regular react based blocks have no issue with underscores

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

The topic ‘Programatically registering ACF field blocks in Gutenberg’ is closed to new replies.