How to create a new reusable block for air-blocks and importer script
Please note: This tutorial is about how to add block to the block import script and not how to create your own blocks! These steps are mostly for Dude staff only.
This tutorial will show you how to create a new block with:
PHP block template assets
SCSS assets
JS assets
SVG assets
Block importer script to be used with newblock
1. Create a block php file
First you'll have to create a new block template to template-parts/blocks/your-block.php:
template-parts/blocks/example.php
<?php/** * The template for example * * Description of your block. * * @Author: Roni Laukkarinen * @Date: 2022-02-10 12:28:36 * @Last Modified by: Roni Laukkarinen * @Last Modified time: 2022-02-10 12:28:36 * * @package airblocks * @link https://developer.wordpress.org/themes/basics/template-files/#template-partials*/namespaceAir_Light;if(empty($title)){maybe_show_error_block('A title is required');return;}?><sectionclass="block block-example"><divclass="container"><!--Yourcontent--></div></section>
2. Create styles for the block
Styles will be located under sass/gutenberg/blocks/:
Then, import the block file to sass/gutenberg/_blocks.scss:
Compile styles either while gulp is up and running or with a single command:
3. Register the block in functions
Next you'll need to open functions.php and search:
After this part, add:
4. Create custom fields for the block
Go to wp-admin, Custom fields > Add New, add fields, select in Location part: Show this field group ifBlockis equal toExample. After this, you will be able to add the block in pages.
5. Create importer script for the block
Use the following template to add sh file under bin/blocks/. Remember to add New files/Dependencies as comments first to clarify things before additions.
Please look for tips in a similar block, because block importers vary a lot. For example carousel has more js additions as where title and content do not have JavaScript at all. Below here is just an example, not a complete one. It makes no sense to create a template about this because scripts vary so much per block.
Add translations to localization.sh if needed. Remember to escape the special characters.
Commit the changes. Add the changes to CHANGELOG.md under [Unreleased]Β tag. Push the changes After this your block should be reusable when running the block importer script: