How to create a next block in the same plugin folder?
Now you have learnt how to create a first block and you have created and completed one and need the next one. Which is the right way, to create a new plugin or to use the same plugin? Both is possible, it depends on the idea of the block. If it’s a completely new thing, create a new plugin with @wordpress/create-block, if it’s the next function for the same theme, create a new plugin for the same theme.
To do this, you need to change the src folder of your first block and let the create-block script help you create the new block.
cd <your-plugin-folder>/src
npx @wordpress/create-block@latest --no-plugin
This should start an assistent for asking you important block information, it should look like this:
Let's add a new block to your existing WordPress plugin:
? The template variant to use for this block: dynamic
? The block slug used for identification (also the output folder name): real-estate-equipment
? The internal namespace for the block name (something unique for your products): dd-commerce
? The display title for your block: Real estate equipment
? The short description for your block (optional): Block for checking real estate equipment like terrasse, pool ...
? The dashicon to make it easier to identify your block (optional): smiley
? The category name to help users browse and discover your block: widgets
Creating a new block in the real-estate-equipment directory.
Creating a "block.json" file.
Done: Block "Real estate equipment" bootstrapped in the real-estate-equipment directory.
Code is Poetry
After this you have to create manually a folder for your first block in src folder and move all files of first block into it:
cd <your-plugin-folder>/src
mkdir "my-first-block-name"
mv block.json my-first-block-name
mv *.js my-first-block-name
mv *.css my-first-block-name
mv *.scss my-first-block-name
mv render.php my-first-block-name
And for creating the same folder in build folder please do npm run start:
npm run start
As last step you have to change path to first block in register_block_type function in plugins php file and add new register_block_type function for register new block:
function wpdocs_create_blocks_mysite_block_init() {
register_block_type( __DIR__ . '/build/block-a' );
register_block_type( __DIR__ . '/build/block-b' );
}
add_action( 'init', 'wpdocs_create_blocks_mysite_block_init' );
And now you can start with development of next block :-)
Thank you Nick Diego for sharing this knowledge in this video: