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
The `register_block_type_from_metadata` function registers the block we are going to create and specifies the `editor_script` file handle registered from the metadata provided in `block.json` file. So now when the editor loads it will load this script.
100
+
The `register_block_type` function registers the block we are going to create and specifies the editor script handle registered from the metadata provided in `block.json` file with the `editorScript` field. So now when the editor loads it will load this script.
101
101
102
102
```json
103
103
{
@@ -137,6 +137,6 @@ For more info, see the build section of the [Getting Started with JavaScript tut
137
137
138
138
## Summary
139
139
140
-
Hopefully, at this point, you have your plugin created and activated. We have the package.json with the `@wordpress/scripts` dependency, that defines the build and start scripts. The basic block is in place and can be added to the editor.
140
+
Hopefully, at this point, you have your plugin created and activated. We have the `package.json` with the `@wordpress/scripts` dependency, that defines the build and start scripts. The basic block is in place and can be added to the editor.
141
141
142
142
Next Section: [Anatomy of a Block](/docs/getting-started/tutorials/create-block/block-anatomy.md)
Copy file name to clipboardExpand all lines: docs/reference-guides/block-api/block-metadata.md
+48-8Lines changed: 48 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,6 @@
1
1
# Metadata
2
2
3
-
To register a new block type using metadata that can be shared between codebase that uses JavaScript and PHP, start by creating a `block.json` file. This file:
4
-
5
-
- Gives a name to the block type.
6
-
- Defines some important metadata about the registered block type (title, category, icon, description, keywords).
7
-
- Defines the attributes of the block type.
8
-
- Registers all the scripts and styles for your block type.
3
+
Starting in WordPress 5.8 release, we encourage using the `block.json` metadata file as the canonical way to register block types. Here is an example `block.json` file that would define the metadata for a plugin create a notice block.
9
4
10
5
**Example:**
11
6
@@ -50,9 +45,19 @@ To register a new block type using metadata that can be shared between codebase
50
45
}
51
46
```
52
47
53
-
The same file is also used when [submitting block to Block Directory](/docs/getting-started/tutorials/create-block/submitting-to-block-directory.md).
48
+
## Benefits using the metadata file
49
+
50
+
The block definition allows code sharing between JavaScript, PHP, and other languages when processing block types stored as JSON, and registering blocks with the `block.json` metadata file provides multiple benefits on top of it.
51
+
52
+
From a performance perspective, when themes support lazy loading assets, blocks registered with `block.json` will have their asset enqueuing optimized out of the box. The frontend CSS and JavaScript assets listed in the `style` or `script` properties will only be enqueued when the block is present on the page, resulting in reduced page sizes.
53
+
54
+
Furthermore, because the [Block Type REST API Endpoint](https://developer.wordpress.org/rest-api/reference/block-types/) can only list blocks registered on the server, registering blocks server-side is recommended; using the `block.json` file simplifies this registration.
55
+
56
+
Last, but not least, the [WordPress Plugins Directory](https://wordpress.org/plugins/) can detect `block.json` files, highlight blocks included in plugins, and extract their metadata. If you wish to [submit your block(s) to the Block Directory](/docs/getting-started/tutorials/create-block/submitting-to-block-directory.md), all blocks contained in your plugin must have a `block.json` file for the Block Directory to recognize them.
54
57
55
-
## Server-side registration
58
+
## Block registration
59
+
60
+
### PHP (server-side)
56
61
57
62
The [`register_block_type`](https://developer.wordpress.org/reference/functions/register_block_type/) function that aims to simplify the block type registration on the server, can read metadata stored in the `block.json` file.
58
63
@@ -75,6 +80,41 @@ register_block_type(
75
80
);
76
81
```
77
82
83
+
### JavaScript (client-side)
84
+
85
+
When the block is registered on the server, you only need to register the client-side settings on the client using the same block’s name.
86
+
87
+
**Example:**
88
+
89
+
```js
90
+
registerBlockType( 'my-plugin/notice', {
91
+
edit: Edit,
92
+
// ...other client-side settings
93
+
} );
94
+
```
95
+
96
+
Although registering the block also on the server with PHP is still recommended for the reasons above, if you want to register it only client-side you can now use `registerBlockType` method from `@wordpress/blocks` package to register a block type using the metadata loaded from `block.json` file.
97
+
98
+
The function takes two params:
99
+
100
+
-`$blockNameOrMetadata` (`string`|`Object`) – block type name (supported previously) or the metadata object loaded from the `block.json` file with a bundler (e.g., webpack) or a custom Babel plugin.
Copy file name to clipboardExpand all lines: docs/reference-guides/block-api/block-registration.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
Block registration API reference.
4
4
5
-
**Note:** You can use the functions documented on this page, but a flexible method to register new block types is to use the block.json metadata file. See [metadata documentation for complete information](/docs/reference-guides/block-api/block-metadata.md).
5
+
**Note:** You can use the functions documented on this page to register a block on the client-side only, but a flexible method to register new block types is to use the `block.json` metadata file. See [metadata documentation for complete information](/docs/reference-guides/block-api/block-metadata.md).
Copy file name to clipboardExpand all lines: docs/reference-guides/filters/block-filters.md
+50-27Lines changed: 50 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,13 +2,56 @@
2
2
3
3
To modify the behavior of existing blocks, WordPress exposes several APIs:
4
4
5
-
## Filters
5
+
## Registration
6
6
7
-
The following filters are available to extend the settings for existing blocks.
7
+
The following filters are available to extend the settings for blocks during their registration.
8
+
9
+
### `block_type_metadata`
10
+
11
+
Filters the raw metadata loaded from the `block.json` file when registering a block type on the server with PHP. It allows applying modifications before the metadata gets processed.
12
+
13
+
The filter takes one param:
14
+
15
+
-`$metadata` (`array`) – metadata loaded from `block.json` for registering a block type.
16
+
17
+
_Example_:
18
+
19
+
```php
20
+
<?php
21
+
22
+
function filter_metadata_registration( $metadata ) {
Filters the settings determined from the processed block type metadata. It makes it possible to apply custom modifications using the block metadata that isn’t handled by default.
34
+
35
+
The filter takes two params:
36
+
37
+
-`$settings` (`array`) – Array of determined settings for registering a block type.
38
+
-`$metadata` (`array`) – Metadata loaded from the `block.json` file.
39
+
40
+
_Example:_
41
+
42
+
```php
43
+
function filter_metadata_registration( $settings, $metadata ) {
Used to filter the block settings. It receives the block settings and the name of the registered block as arguments. Since v6.1.0 this filter is also applied to each of a block's deprecated settings.
54
+
Used to filter the block settings when registering the block on the client with JavaScript. It receives the block settings and the name of the registered block as arguments. This filter is also applied to each of a block's deprecated settings.
12
55
13
56
_Example:_
14
57
@@ -34,6 +77,10 @@ wp.hooks.addFilter(
34
77
);
35
78
```
36
79
80
+
## Block Editor
81
+
82
+
The following filters are available to change the behavior of blocks while editing in the block editor.
83
+
37
84
### `blocks.getSaveElement`
38
85
39
86
A filter that applies to the result of a block's `save` function. This filter is used to replace or extend the element, for example using `wp.element.cloneElement` to modify the element's props or replace its children, or returning an entirely new element.
@@ -221,30 +268,6 @@ wp.hooks.addFilter(
221
268
222
269
{% end %}
223
270
224
-
#### `media.crossOrigin`
225
-
226
-
Used to set or modify the `crossOrigin` attribute for foreign-origin media elements (i.e `<img>`, `<audio>` , `<img>` , `<link>` , `<script>`, `<video>`). See this [article](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin) for more information the `crossOrigin` attribute, its values and how it applies to each element.
227
-
228
-
One example of it in action is in the Image block's transform feature to allow cross-origin images to be used in a `<canvas>`.
229
-
230
-
_Example:_
231
-
232
-
```js
233
-
addFilter(
234
-
'media.crossOrigin',
235
-
'my-plugin/with-cors-media',
236
-
// The callback accepts a second `mediaSrc` argument which references
237
-
// the url to actual foreign media, useful if you want to decide
238
-
// the value of crossOrigin based upon it.
239
-
( crossOrigin, mediaSrc ) => {
240
-
if ( mediaSrc.startsWith( 'https://example.com' ) ) {
Copy file name to clipboardExpand all lines: docs/reference-guides/filters/editor-filters.md
+24Lines changed: 24 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,6 +38,30 @@ wp.hooks.addFilter(
38
38
);
39
39
```
40
40
41
+
### `media.crossOrigin`
42
+
43
+
Used to set or modify the `crossOrigin` attribute for foreign-origin media elements (i.e `<img>`, `<audio>` , `<img>` , `<link>` , `<script>`, `<video>`). See this [article](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin) for more information the `crossOrigin` attribute, its values and how it applies to each element.
44
+
45
+
One example of it in action is in the Image block's transform feature to allow cross-origin images to be used in a `<canvas>`.
46
+
47
+
_Example:_
48
+
49
+
```js
50
+
addFilter(
51
+
'media.crossOrigin',
52
+
'my-plugin/with-cors-media',
53
+
// The callback accepts a second `mediaSrc` argument which references
54
+
// the url to actual foreign media, useful if you want to decide
55
+
// the value of crossOrigin based upon it.
56
+
( crossOrigin, mediaSrc ) => {
57
+
if ( mediaSrc.startsWith( 'https://example.com' ) ) {
Copy file name to clipboardExpand all lines: packages/create-block-tutorial-template/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,11 @@
2
2
3
3
## Unreleased
4
4
5
+
### Enhancement
6
+
7
+
- Plugin scaffolded requires WordPress 5.8 now ([#33252](https://github.com/WordPress/gutenberg/pull/33252).
8
+
- Scaffolded block is now registered from `block.json` with the `register_block_type` helper ([#33252](https://github.com/WordPress/gutenberg/pull/33252)).
Copy file name to clipboardExpand all lines: packages/create-block/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,11 @@
2
2
3
3
## Unreleased
4
4
5
+
### Enhancement
6
+
7
+
- Plugin scaffolded with the `esnext` template requires WordPress 5.8 now ([#33252](https://github.com/WordPress/gutenberg/pull/33252).
8
+
- Block scaffolded with the `esnext` template is now registered from `block.json` with the `register_block_type` helper ([#33252](https://github.com/WordPress/gutenberg/pull/33252)).
0 commit comments