Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

130
Visualizações
Adding innerBlock with attributes on click in Wordpress Gutenberg Editor

I am creating a gutenberg block with innerblocks. In this case the native core/cover block. I then add three innerblocks with the TEMPLATE variable, and load it in to <InnerBlocks ...>

My issue is, that when you add another cover block via editor, it does not have the innerBlockPresets which i defined. As far as I can tell allowedBlocks doesn't allow attributes.

I have to use the native cover block for this. How can I solve this?

  • Wordpress 5.9
  • base created with @wordpress/create-block

edit.js

import {useBlockProps, InnerBlocks} from '@wordpress/block-editor';

export default function Edit(}) {

    const blockProps = useBlockProps();
    const ALLOWED_BLOCKS = ['core/cover'];
    const innerBlockPresets = {'className': 'is-awesome', 'align': 'full'};
    const TEMPLATE = [['core/cover', innerBlockPresets], ['core/cover', innerBlockPresets], ['core/cover', innerBlockPresets]];

    return (
        <div {...blockProps}>
                <InnerBlocks
                    allowedBlocks={ALLOWED_BLOCKS}
                    template={TEMPLATE}
                    renderAppender={() => <InnerBlocks.ButtonBlockAppender/>}
                />
        </div>
    );
}

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

A possible solution could be to register a block variation of core/cover, eg:

index.js

wp.blocks.registerBlockVariation('core/cover', {
    name: 'cover-variation',
    title: 'Cover Variation',
    icon: 'star-filled',
    attributes: {
        className: 'is-awesome',
        align: 'full'
    }
});

This enables you to have a predefined set of attributes. Next, if ALLOWED_BLOCKS in InnerBlocks contains core/cover, only core/cover & your cover-variation (or any other variations of cover) can be added, eg:

edit.js

import { __ } from '@wordpress/i18n';
import { useBlockProps, InnerBlocks } from '@wordpress/block-editor';

export default function Edit() {
    const ALLOWED_BLOCKS = ['core/cover'];
    const TEMPLATE = [
        ['core/cover', {
            className: 'is-awesome',
            align: 'full'
        }]];

    return (
        <div {...useBlockProps()}>
            <InnerBlocks
                allowedBlocks={ALLOWED_BLOCKS}
                template={TEMPLATE}
            />
        </div>
    );
}

enter image description here

An issue I've found with this approach is the variation cannot be set in TEMPLATE, as the block variation is registered after the core block but too late for InnerBlock template validation. If you try this, the InnerBlock template validation returns "undefined" when using a block variation and won't render. So to avoid this issue, in the TEMPLATE, set the same properties as your variation.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda