Customizing WP React Blocks
Prerequisites​
- Node.js LTS or above (which can be checked by running
node -v).
Setup​
We provide a sample template that you can use to get started.
You can initialize the project by running the following command in the custom folder:
- npm
- pnpm
- Yarn
- Bun
npx @devgateway/create-wp-customizer wp-customizer
pnpm dlx @devgateway/create-wp-customizer wp-customizer
yarn dlx @devgateway/create-wp-customizer wp-customizer
bun x @devgateway/create-wp-customizer wp-customizer
@devgateway/create-wp-customizer contains two templates that you can use to create a new block.
template-js- This template is for creating a new block using JavaScript.template-ts- This template is for creating a new block using TypeScript.
You can choose the template you want to use when initializing the project.
This will create a new folder called wp-customizer in the custom folder.
Running the project​
- Add the
wp-customizerfolder as a volume in yourdocker-compose.yamlfile.
wordpress:
volumes:
- ./custom/wp-customizer:/var/www/html/wp-content/plugins/wp-customizer-blocks
- Install NPM dependencies in the
custom/wp-customizer/blocksdirectory.
- npm
- pnpm
- Yarn
- Bun
npm install
pnpm install
yarn install
bun install
- Install the
@devgateway/dvz-wp-commonspackage in thecustom/wp-customizer/blocksdirectory.
- npm
- pnpm
- Yarn
- Bun
npm install @devgateway/dvz-wp-commons@latest
pnpm add @devgateway/dvz-wp-commons@latest
yarn add @devgateway/dvz-wp-commons@latest
bun add @devgateway/dvz-wp-commons@latest
- Run the development server in the
custom/wp-customizer/blocksdirectory.
- npm
- pnpm
- Yarn
- Bun
npm run start
pnpm run start
yarn run start
bun run start
- Activate the plugin in the WordPress dashboard - This will create a new wordpress plugin in the
wp-content/pluginsdirectory.
Folder Structure​
The folder structure will look like this:
custom/wp-customizer
├── blocks
│ ├── editor.css
│ ├── example
│ │ ├── BlockEdit.jsx
│ │ ├── BlockSave.jsx
│ │ └── index.js
│ ├── index.js
│ ├── index.php
│ ├── package.json
│ ├── style.css
│ └── webpack.config.js
Customizing Blocks​
NB: When creating custom blocks, ensure that the block name is unique and does not conflict with any existing block even in the Wordpress base image.
You can customize the blocks by editing the BlockEdit.js and BlockSave.js files in the blocks/block-1 directory.
BlockEdit.js​
The BlockEdit.js file contains the JSX code for the block in the editor.
Extending the BlockEdit component can be extended using the following React Classes that are provided by the @devgateway/dvz-wp-commons package.
ComponentWithSettings- Provides base functionality for blocks requiring integration with an iframe and WordPress settings. Listens for events and sends messages to an iframe.BlockEditWithFilters- Extends ComponentWithSettings to add filtering functionality. Allows users to filter posts based on type, taxonomy, and categories.BlockEditWithAPIMetadata- Extends ComponentWithSettings to integrate metadata from external APIs. Dynamically loads dimensions, filters, measures, and categories for selected apps.
BlockSave.js​
The BlockSave.js file contains the JSX code for the block in the frontend.
Registering Blocks​
To register the block, you need to add the following code to the index.js file:
import './example/index.js';
This will register the block in the WordPress editor.
Building Blocks​
To build the blocks, run the following command in the front/wordpress/wp-react-blocks-plugin/blocks directory:
- npm
- pnpm
- Yarn
- Bun
npm run build
pnpm run build
yarn build
bun run build