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 generator for reusable patterns will prompt you for a name, description, and categories for the pattern, then create a script to register a reusable pattern with metadata based on your inputs and instructions for how to create the markup for the pattern.
356
356
@@ -364,6 +364,45 @@ The following file will be created based on your input:
The generator for custom blocks plugins will prompt you for a plugin name and description, WordPress and PHP versions (for compatibility info), and author. It uses this info to scaffold the configuration and readme files for a custom blocks plugin that initially does not have any blocks.
370
+
371
+
```sh
372
+
npm run generate:custom-blocks-plugin
373
+
```
374
+
375
+
The following files will be created based on your input:
376
+
377
+
-`src/plugins/<plugin-name>/src/.gitkeep`
378
+
-`src/plugins/<plugin-name>/<plugin-name>.php`
379
+
-`src/plugins/<plugin-name>/readme.txt`
380
+
381
+
It will also modify these files to automatically updated the build/development processes and configuration:
382
+
383
+
-`package.json`
384
+
-`docker-compose.yml`
385
+
386
+
### Custom Block
387
+
388
+
The generator for custom blocks will prompt you for the plugin that the block should belong to, a block name and description, and whether the block needs a `view.js` file for client-side JS. Note: this should only be run after a custom blocks plugin has been generated from `npm run generate:custom-blocks-plugin`.
389
+
390
+
```sh
391
+
npm run generate:custom-block
392
+
```
393
+
394
+
The following files will be created based on your input:
See [custom block structure](#custom-block-structure) for more info on what these files are for.
405
+
367
406
## Plugins
368
407
369
408
### Installing Plugins
@@ -402,26 +441,52 @@ This is a non-comprehensive list of plugins that we have found useful on other p
402
441
403
442
## Custom Blocks
404
443
405
-
We have a plugin for custom blocks called `example-blocks`, which lives in `src/plugins`. For the blocks to be available in WordPress, you must activate the "Example Blocks" plugin from the WordPress admin's plugins page.
444
+
We have two [generators](#generators) that can be used in tandem to create the necessary scaffolding for custom blocks. The first is `npm run generate:custom-blocks-plugin`, which should be run first to create the plugin config, readme, directory, and `package.json`/`docker-compose.yml` changes necessary to make the plugin available to WordPress. The second is `npm run generate:custom-block`, which creates the boilerplate files necessary to create a single custom block within the plugin.
445
+
446
+
Note: you will need to restart your development process to pick up the changes after adding a custom blocks plugin and/or a custom block.
447
+
448
+
Once you have created a custom blocks plugin that has at least one custom block, you should be able to activate it in the WordPress admin page for Plugins.
449
+
450
+
The custom blocks plugin generator should handle creating the npm scripts for you, but the general format is as follows:
451
+
452
+
```json
453
+
"plugins:dev": "run-p plugins:dev:* || echo \"Unable to build plugins\"",
454
+
"plugins:build": "run-s plugins:build:* || echo \"Unable to build plugins\"",
The `plugins:dev` and `plugins:build` scripts will automatically pick up any `plugins:dev:*` and `plugins:build:*` scripts that get added, minimizing the maintenance overhead from adding more plugins.
460
+
461
+
Similarly, the `docker-compose.yml` volume mapping should automatically be updated by the generator, but if not, each plugin needs to be mapped to a folder within the container's `/var/www/html/wp-content/plugins/<plugin-name>` folder, like so:
Each custom block will have most, if not all, of the following files:
406
473
407
-
The plugins can be built with `npm run plugins:dev` or `npm run plugins:build`, but that generally shouldn't be necessary, since those scripts are run as part of the standard `npm start` and `npm run build:prod` scripts.
474
+
- `block.json`: configuration/metadata for the block
475
+
- `edit.js`: the component used while editing
476
+
- `editor.scss`: custom styles for the editor view
477
+
- `index.js`: entry point for the JS bundle
478
+
- `save.js`: the component rendered on the site
479
+
- `style.scss`: custom styles for the block when rendered on the site
480
+
- `view.js`: any JS that needs to run when the block is rendered on a non-admin page (optional)
408
481
409
-
### Creating a New Custom Block
482
+
It's important to note that while `save.js` is written like a React component, it does not have reactivity when rendered on the site. The React component is used to serialize HTML that is sent to the client from the server, so hooks like `useEffect` will not run when the component is rendered. If your component requires JS for its functionality, you need to provide that JS in the `view.js` file.
410
483
411
-
Follow these steps to create a new custom block and wire it up with the normal development/build processes:
484
+
Once the boilerplate files have been created, follow these steps to build out the custom block to fit your needs.
412
485
413
-
1. Create a new folder at `src/plugins/example-blocks/src/<block-name>`
414
-
1. Either copy files from another block or manually create these files:
415
-
-`block.json`: configuration/metadata for the block
416
-
-`index.js`: entry point for the JS bundle
417
-
-`edit.js`: the component used while editing
418
-
-`save.js`: the component rendered on the site
419
-
-`view.js`: any JS that needs to run when the block is rendered on a non-admin page (optional)
420
-
-`editor.scss`: custom styles for the editor view
421
-
-`style.scss`: custom styles for the block when rendered on the site
422
-
1. Configure the custom block by updating `block.json`, namely the `name`, `title`, `icon`, and `description` fields. If you don't need a `view.js` file, delete the `viewScript` key.
423
-
1. Implement the edit function, which will usually be form controls corresponding to attributes that you define in `index.js`
486
+
1. Configure the custom block by [updating `block.json`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/). Depending on how you answered prompts from the generator, this may be mostly done. You'll likely want to update the `icon` field with a [dashicon name](https://developer.wordpress.org/resource/dashicons)
487
+
1. Implement the edit function, which will control how the block is rendered/created in the Gutenberg editor
424
488
1. Implement the save function, which will consume the attributes defined in `index.js` and render the block's desired markup
489
+
1. Implement the front-end JS for the component in `view.js` if needed
0 commit comments