You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on May 2, 2022. It is now read-only.
Is your enhancement related to a problem? Please describe.
The theme scaffold provides a very basic if slightly confusion structure for custom blocks. Currently there are two locations for the blocks; assets/js/blocks and includes/blocks. There is not much in the way of direction as to how to work with these directories.
Wordpress 5.5 introduced new API's for working with blocks and I think we should take this opportunity to both update the scaffold to use them and to standardize how we build blocks.
Describe the solution you'd like
The main API change that came with 5.5 was the introduction of block.json for defining a block type metadata and register_block_type_from_metatdata for registering blocks in PHP.
The register_block_type_from_metatdata functions first parameter is the path to the block.json file for this block which contains all of the metadata for the block.
As it stands now, registering a dynamic block has two un-related steps:
Register the custom block in php using register_block_type
Register the custom block in JavaScript using registerBlockType and enqueue the related file.
If our blocks are only registered on the PHP using the new register_block_type_from_metatdata function , we can register blocks in a single step as this function automatically registers and enqueues the script/styles.
Build Changes
Instead of generating a single block-editor.js file that holds all of the code to register all of the blocks, we will need to add an entry points for JS and CSS to the config/webpack.settings.js file the represent the JS/CSS that would be loaded into th e block editor.
The register_block_type_from_metatdata also looks for an *.assets.php file to manage dependencies, as such we'll need to leverage either the @wordpress/scripts package or the DependencyExtractionPlugin see #185.
Directory Structure Changes
I propose that we remove the assets/js/blocks directory and place all custom blocks in includes/blocks/{block-name}.
block.json
This file holds the meta data for the block.
register.php
This file is pulled into includes/blocks.php and holds the register_block_from_metadata call and the register_callback callback.
register.js
This file handles the JavaScript side of register a block and imports items from block.json as needed. This is the file that is targeted by the entrypoint in weback.
edit.js
Contains the Edit component for the block
save.js
Contains the Save component for the block.
props-shape.js
Contains the prop-types definitions for the block
inspector-controls.js
Contains the InspectorControl items for the block.
markup.php
This file contains the markup for the block that is rendered on the front end. It is used the register_callback in register.php
Additional context
There is an internal project for custom blocks that leverages a similar set up that will also provide a means for registering new blocks.
The text was updated successfully, but these errors were encountered:
Is your enhancement related to a problem? Please describe.
The theme scaffold provides a very basic if slightly confusion structure for custom blocks. Currently there are two locations for the blocks;
assets/js/blocks
andincludes/blocks
. There is not much in the way of direction as to how to work with these directories.Wordpress 5.5 introduced new API's for working with blocks and I think we should take this opportunity to both update the scaffold to use them and to standardize how we build blocks.
Describe the solution you'd like
The main API change that came with 5.5 was the introduction of
block.json
for defining a block type metadata andregister_block_type_from_metatdata
for registering blocks in PHP.The
register_block_type_from_metatdata
functions first parameter is the path to theblock.json
file for this block which contains all of the metadata for the block.For example:
The items in the above example should look familiar as they are many of the same items passed to
registerBlockType
on the JavaScript side.The exciting part is this:
As it stands now, registering a dynamic block has two un-related steps:
register_block_type
registerBlockType
and enqueue the related file.If our blocks are only registered on the PHP using the new
register_block_type_from_metatdata
function , we can register blocks in a single step as this function automatically registers and enqueues the script/styles.Build Changes
Instead of generating a single
block-editor.js
file that holds all of the code to register all of the blocks, we will need to add an entry points for JS and CSS to theconfig/webpack.settings.js
file the represent the JS/CSS that would be loaded into th e block editor.The
register_block_type_from_metatdata
also looks for an*.assets.php
file to manage dependencies, as such we'll need to leverage either the@wordpress/scripts
package or theDependencyExtractionPlugin
see #185.Directory Structure Changes
I propose that we remove the
assets/js/blocks
directory and place all custom blocks inincludes/blocks/{block-name}
.block.json
This file holds the meta data for the block.
register.php
This file is pulled into
includes/blocks.php
and holds theregister_block_from_metadata
call and theregister_callback
callback.register.js
This file handles the JavaScript side of register a block and imports items from
block.json
as needed. This is the file that is targeted by the entrypoint in weback.edit.js
Contains the Edit component for the block
save.js
Contains the Save component for the block.
props-shape.js
Contains the prop-types definitions for the block
inspector-controls.js
Contains the InspectorControl items for the block.
markup.php
This file contains the markup for the block that is rendered on the front end. It is used the
register_callback
inregister.php
Additional context
There is an internal project for custom blocks that leverages a similar set up that will also provide a means for registering new blocks.
The text was updated successfully, but these errors were encountered: