-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Docs: Fundamentals of Block Development - Registration of a block #56334
Merged
juanmaguitar
merged 26 commits into
trunk
from
fundamentals-block-development/registration-of-a-block
Nov 28, 2023
Merged
Changes from 21 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
6355ef2
Add block registration documentation
juanmaguitar d6277a6
Update block registration process
juanmaguitar ff1cef8
Update block registration in client code
juanmaguitar 413fbbf
updated doc
juanmaguitar 7dd94ea
Update block registration settings
juanmaguitar 83f4fdb
Grammar check
juanmaguitar e436065
Moved Registration of A block to fundamentals of block development
juanmaguitar 6450388
Added maarkdown extension
juanmaguitar af24db7
Delete docs/getting-started/fundamentals-block-development/registrati…
juanmaguitar 64f131f
Removed "key"
juanmaguitar a2b7d0a
clarification path block.json
juanmaguitar 454ccd7
Fix block.json registration path
juanmaguitar b5ecd9a
server-side features require block server registration
juanmaguitar 33a8ea8
server-side features require block server registration
juanmaguitar 4f6fb44
Update docs/getting-started/fundamentals-block-development/registrati…
juanmaguitar 41e9101
Update docs/getting-started/fundamentals-block-development/registrati…
juanmaguitar 66ff132
Update docs/getting-started/fundamentals-block-development/registrati…
juanmaguitar 0245933
Update docs/getting-started/fundamentals-block-development/registrati…
juanmaguitar e0954ec
Update docs/getting-started/fundamentals-block-development/registrati…
juanmaguitar 4ee9abc
Update docs/getting-started/fundamentals-block-development/registrati…
juanmaguitar 45ef201
Fix formatting in registration-of-a-block.md
juanmaguitar 965aa74
Merge branch 'trunk' into fundamentals-block-development/registration…
juanmaguitar 7e152a3
Added link to new page and some text adjustments
juanmaguitar f56a645
Update block registration function and build process reference
juanmaguitar de5bb8a
Merge branch 'trunk' into fundamentals-block-development/registration…
juanmaguitar 9bcd5ba
Merge branch 'trunk' into fundamentals-block-development/registration…
juanmaguitar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
docs/getting-started/fundamentals-block-development/registration-of-a-block.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# Registration of a block | ||
|
||
A block is usually registered through a plugin on both the server and client-side using its `block.json` metadata. | ||
|
||
Although technically, blocks could be registered only in the client, **registering blocks on both the server and in the client is a strong recommendation**. Some server-side features like Dynamic Rendering, Block Supports, Block Hooks, or Block style variations require the block to "exist" on the server, and they won't work properly without server registration of the block. | ||
|
||
For example, to allow a block [to be styled via `theme.json`](https://developer.wordpress.org/themes/global-settings-and-styles/settings/blocks/), it needs to be registered on the server, otherwise, any styles assigned to it in `theme.json` will be ignored. | ||
|
||
[![Open Block Registration diagram in excalidraw](https://developer.wordpress.org/files/2023/11/block-registration-e1700493399839.png)](https://excalidraw.com/#json=PUQu7jpvbKsUHYfpHWn7s,61QnhpZtjykp3s44lbUN_g "Open Block Registration diagram in excalidraw") | ||
|
||
### Registration of the block with PHP (server-side) | ||
|
||
Block registration on the server usually takes place in the main plugin PHP file with the `register_block_type` function called on the [init hook](https://developer.wordpress.org/reference/hooks/init/). | ||
|
||
The [`register_block_type`](https://developer.wordpress.org/reference/functions/register_block_type/) function aims to simplify block type registration on the server by reading metadata stored in the `block.json` file. | ||
|
||
This function takes two params relevant in this context (`$block_type` accepts more types and variants): | ||
|
||
- `$block_type` (`string`) – path to the folder where the `block.json` file is located or full path to the metadata file if named differently. | ||
- `$args` (`array`) – an optional array of block type arguments. Default value: `[]`. Any arguments may be defined. However, the one described below is supported by default: | ||
- `$render_callback` (`callable`) – callback used to render blocks of this block type, it's an alternative to the `render` field in `block.json`. | ||
|
||
As part of the build process, the `block.json` file is usually copied from the `src` folder to the `build` folder, so the path to the `block.json` of your registered block should refer to the `build` folder. | ||
|
||
`register_block_type` returns the registered block type (`WP_Block_Type`) on success or `false` on failure. | ||
|
||
**Example:** | ||
```php | ||
register_block_type( | ||
__DIR__ . '/notice', | ||
array( | ||
'render_callback' => 'render_block_core_notice', | ||
) | ||
); | ||
``` | ||
|
||
**Example:** | ||
```php | ||
function minimal_block_ca6eda___register_block() { | ||
register_block_type( __DIR__ . '/build' ); | ||
} | ||
|
||
add_action( 'init', 'minimal_block_ca6eda___register_block' ); | ||
``` | ||
_See the [full block example](https://github.com/WordPress/block-development-examples/tree/trunk/plugins/minimal-block-ca6eda) of the [code above](https://github.com/WordPress/block-development-examples/blob/trunk/plugins/minimal-block-ca6eda/index.php)_ | ||
|
||
### Registration of the block with JavaScript (client-side) | ||
|
||
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. | ||
|
||
**Example:** | ||
|
||
```js | ||
registerBlockType( 'my-plugin/notice', { | ||
edit: Edit, | ||
// ...other client-side settings | ||
} ); | ||
``` | ||
|
||
Although registering the block also on the server with PHP is still recommended for the reasons mentioned at ["Benefits using the metadata file"](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#benefits-using-the-metadata-file), if you want to register it only client-side you can use [`registerBlockType`](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-blocks/#registerblocktype) method from `@wordpress/blocks` package to register a block type using the metadata loaded from `block.json` file. | ||
|
||
The function takes two params: | ||
|
||
- `$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. | ||
- `$settings` (`Object`) – client-side block settings. | ||
|
||
The client-side block settings object passed as a second parameter include two properties that are especially relevant: | ||
- `edit`: The React component that gets used in the editor for our block. | ||
- `save`: The React component that generates the static HTML markup that gets saved to the Database. | ||
|
||
|
||
`registerBlockType` returns the registered block type (`WPBlock`) on success or `undefined` on failure. | ||
|
||
**Example:** | ||
|
||
```js | ||
import { registerBlockType } from '@wordpress/blocks'; | ||
import metadata from './block.json'; | ||
|
||
registerBlockType( metadata.name, { | ||
edit() { | ||
return <p>Hello World - Block Editor</p>; | ||
}, | ||
save() { | ||
return <p>Hello World - Frontend</p>; | ||
}, | ||
} ); | ||
``` | ||
_See the [code above](https://github.com/WordPress/block-development-examples/blob/trunk/plugins/minimal-block-ca6eda/src/index.js) in [an example](https://github.com/WordPress/block-development-examples/tree/trunk/plugins/minimal-block-ca6eda)_ | ||
|
||
## Additional resources | ||
|
||
- [`register_block_type` PHP function](https://developer.wordpress.org/reference/functions/register_block_type/) | ||
- [`registerBlockType` JS function](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-blocks/#registerblocktype) | ||
- [Why a block needs to be registered in both the server and the client?](https://github.com/WordPress/gutenberg/discussions/55884) | GitHub Discussion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be valuable to link the newly created documentation page, so it's clear that
block.json
still needs to be wired with PHP and can be used in JavaScript if folks needs access to the same properties.