Register Block Type #63695
-
Hi! I'm a new block developer and I'm facing a problem registering my 2nd block on this new plugin. in my PHP function create_block_carmo_order_form_to_cart_block_init() {
register_block_type(__DIR__ . '/build/block.json');
register_block_type(__DIR__ . '/build/block2.json');
}
add_action('init', 'create_block_carmo_order_form_to_cart_block_init'); As soon as I place this call to block2.json (my code is compiled with no errors btw) I get a warning on WP frontend: and a console log warning twice: my block.json files are normal. same namespace, everything seems ok. Block.js {
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "carmo-order-form-to-cart/category",
"version": "0.1.0",
"title": "List Products by Category",
"category": "carmo-blocks",
"icon": "cart",
"description": "Display products from a specific product category",
"example": {},
"supports": {
"color": {
"background": true,
"text": true
},
"html": false,
"typography": {
"fontSize": true
}
},
"attributes": {
"selectedCategory":{
"type": "string",
"default": "0"
},
"showInStock":{
"type": "boolean",
"default": "0"
},
"imageWidth": {
"type": "string",
"default": "100"
}
},
"textdomain": "carmo-order-form-to-cart",
"editorScript": "file:./index.js",
"editorStyle": "file:./index.css",
"style": "file:./style-index.css",
"render": "file:./render.php",
"viewScript": "file:./view.js"
} Block2.js {
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "carmo-order-form-to-cart/globalupdatecart",
"version": "0.1.0",
"title": "Global Update Cart",
"category": "carmo-blocks",
"icon": "cart",
"description": "Display a global Update button",
"example": {},
"supports": {
"color": {
"background": true,
"text": true
},
"html": false,
"typography": {
"fontSize": true
}
},
"textdomain": "carmo-order-form-to-cart",
"editorScript": "file:./index.js",
"editorStyle": "file:./index.css",
"style": "file:./style-index.css",
"render": "file:./render2.php"
} my index.js /**
* Registers a new block provided a unique name and an object defining its behavior.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/
*/
import { registerBlockType } from '@wordpress/blocks';
/**
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
* All files containing `style` keyword are bundled together. The code used
* gets applied both to the front of your site and to the editor.
*
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
*/
import './style.scss';
/**
* Internal dependencies
*/
import MainEdit from './Main/edit';
import MainSave from './Main/save';
import MainMetadata from './block.json';
import GlobalUpdateCartEdit from './GlobalUpdateCart/edit';
import GlobalUpdateCartSave from './GlobalUpdateCart/save';
import GlobalUpdateCartMetadata from './block2.json';
import { __ } from '@wordpress/i18n';
/**
* Every block starts by registering a new block type definition.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/
*/
registerBlockType( MainMetadata.name,{
edit: MainEdit,
save: MainSave,
},
);
registerBlockType( GlobalUpdateCartMetadata.name, {
edit: GlobalUpdateCartEdit,
save: GlobalUpdateCartSave,
},
); can anyone give me a clue? point me in the right direction? thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
My guess is that this file doesn't exist. Generally, when building multiple blocks, the structure will look something like this:
Then you should be able to register the block like this: function create_block_carmo_order_form_to_cart_block_init() {
register_block_type(__DIR__ . '/build/Main');
register_block_type(__DIR__ . '/build/GlobalUpdateCart');
}
add_action('init', 'create_block_carmo_order_form_to_cart_block_init'); |
Beta Was this translation helpful? Give feedback.
My guess is that this file doesn't exist. Generally, when building multiple blocks, the structure will look something like this:
Then you should be able to register the block like this: