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
We find ourselves increasingly creating a bunch of plugins that add one or more blocks
4
-
to the block editor.
3
+
We find ourselves increasingly creating a bunch of plugins that add one or more blocks to the block editor.
5
4
6
-
This requires a few functions to load and register. And since we use the same scaffold
7
-
almost any time, it makes sense to split out the block part into a library.
5
+
This requires a few functions to load and register. And since we use the same scaffold almost any time, it makes sense to split out the block part into a library.
8
6
9
-
The library consists of a simple trait for making the plugin support blocks and an abstract class
10
-
for a base server-side rendered block.
7
+
The library consists of a simple trait for making the plugin support blocks and two abstract classes for a base server-side rendered block.
11
8
12
9
## Getting started
13
10
14
-
All you need to do is include the `Has_Blocks` trait in the plugin class, add the loader
15
-
with your custom prefix when initializing the plugin, and finally, add
16
-
the blocks by implementing the `blocks`method.
11
+
All you need to do is include the `Has_Blocks` trait in the plugin class as long as you are using the latest version of our plugin base, which recognizes this trait automatically.
12
+
13
+
Blocks are assumed to be placed in a `blocks`subfolder, and they are being built to `dist/blocks`.
17
14
18
15
**This trait relies on an autoloader being used in the plugin!**
19
16
@@ -24,97 +21,25 @@ class My_Plugin {
24
21
25
22
use Has_Blocks;
26
23
27
-
public function __construct() {
28
-
29
-
// Load blocks.
30
-
$this->load_blocks( 'my-block-prefix' );
31
-
32
-
}
33
-
34
-
public function blocks() {
35
-
36
-
// Full JS only block.
37
-
$this->add_block( 'my-js-block-name' );
38
-
39
-
// Server side rendered block, class extends abstract "Block" class.
Each block requires a list of script dependencies when loaded. These are typically the core WordPress block library scripts,
70
-
or any other script dependency that we need.
71
-
72
-
To customize this, add a `dependencies => []` section to the block arguments array when adding the block. A standard block would then look like this:
73
-
74
-
```
75
-
$this->add_block( 'my-js-block-name', [
76
-
'dependencies' => [
77
-
'wp-i18n'
78
-
],
79
-
] );
80
-
```
81
-
82
-
If you do not set dependencies for a specific block, the global dependency list will be used.
83
-
84
-
By default, the global dependency list contains a most-used set of WP scripts. To modify it, you can ovverride the `protected static $block_dependencies = []` class property.
85
-
86
-
```
87
-
/**
88
-
* Packages that are loaded as dependencies for the blocks globally,
89
-
* unless the block specifies its own dependency list.
90
-
*
91
-
* @var string[]
92
-
*/
93
-
protected static $block_dependencies = [
94
-
'wp-blocks',
95
-
'wp-components',
96
-
'wp-element',
97
-
'wp-i18n',
98
-
'wp-block-editor',
99
-
];
100
-
```
101
-
102
27
# Required Folder Structure
103
28
104
29
The trait assumes the following folder structure:
105
30
106
-
`dist/` is the location of built JavaScript blocks, named with the same name as you add the block with using `add_block()`.
31
+
`blocks/` is the location of all blocks with one folder per block. The `index.js` file in each block folder is the entrypoint for the build script.
32
+
33
+
`dist/blocks` is the location of built JavaScript blocks with the same filename as the main folder name.
107
34
108
35
`languages/` is the location of the translation files. The handle and domain are both set to `{$block_prefix}-{$block_name}`.
109
36
110
37
# Required Methods
111
38
112
-
The trait relies on four methods to be implemented, outside of `blocks()`. These are:
39
+
The trait relies on two methods to be implemented.
113
40
114
-
-`get_version()` that should return a string of the current plugin version. Used for versioning the blocks.
115
-
-`get_url()` should return the URL to the plugin directory.
116
-
-`get_path()` should return the path to the plugin directory.
117
-
-`get_textdomain()` should return the plugin textdomain.
41
+
-`get_url()` should return the URL to the plugin directory.
42
+
-`get_path()` should return the path to the plugin directory.
118
43
119
44
# Hooks & Filters
120
45
@@ -124,6 +49,5 @@ Since this is a composer package, filters are housed inside of the "package name
124
49
125
50
## Filters
126
51
127
-
`bm_block_support_{$BLOCKNAME}_attributes`. Available for any dynamic block that extends `Block` and is reliant on `Block::$name` being set in the consuming dynamic block class. Allows you to customize the specific block attributes.
128
-
129
-
`bm_block_support_{$BLOCKNAME}_wrapper_args`. Available for any dynamic block that extends `Block` and is reliant on `Block::$name` being set in the consuming dynamic block class. Allows you to customize the args sent to `get_block_wrapper_attributes`.
52
+
`bm_block_support_{$BLOCKNAME}_wrapper_args`. Available for any dynamic block that extends `Block` and is reliant on `Block::$name` being set in the consuming dynamic block class.
53
+
Allows you to customize the args sent to `get_block_wrapper_attributes`.
0 commit comments