InnerBlocks format converter | WordPress.org

[ad_1]

Hi,

I noticed that WP has 2 available formats to define InnerBlocks formats – I’d like to know if anyone is aware if there is any function that I could use to convert between those.

Here’re examples:

In blocks.json to define example I have to use this format:

"example": {
    "innerBlocks": [
      {
        "name": "core/heading",
        "attributes": {
          "content": "<h2>Heading</h2>",
          "level": 2
        }
      },
      {
        "name": "core/paragraph",
        "attributes": {
          "content": "<p>Paragraph</p>"
        }
      }
    ]
  }

However to recreate same in <InnerBlocks /> component I have to use this format:

<InnerBlocks
  template={ [
    [
      'core/heading',
      {
        level: 2,
        content: '<h2>Heading</h2>',
      },
    ],
    [
      'core/paragraph',
      {
        content: '<p>Paragraph</p>',
      },
    ],
  ] }
/>

At the end I’d like to reuse code from block.json by simply importing it to JS file and using it like this:

import blockJson from '../block.json';
const blockExample = blockJson.example;

// ... rest of code

<InnerBlocks template={ blockExample.innerBlocks } />

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer