A flexible WordPress plugin for creating custom Gutenberg blocks. This plugin provides a structured foundation for building and managing multiple custom blocks using modern WordPress development practices.
- Modular architecture for creating and maintaining multiple blocks
- Built with WordPress Scripts and Webpack for modern development
- Modern block.json metadata approach for block registration
- SCSS styling with separate editor and frontend styles
- Block inspector controls for customizing block appearance
- Organized codebase following WordPress best practices
custom-blocks/ # Plugin root directory
├── build/ # Compiled assets (generated)
├── src/ # Source files
│ ├── index.js # Main entry point
│ └── blocks/ # Individual blocks
│ └── sample-block/ # Sample block implementation
│ ├── block.json # Block metadata
│ ├── index.js # Block registration
│ ├── edit.js # Edit component
│ ├── save.js # Save component
│ ├── style.scss # Frontend styles
│ └── editor.scss # Editor-only styles
├── custom-blocks.php # Main plugin file
├── package.json # NPM configuration
├── webpack.config.js # Webpack configuration
└── README.md # This documentation
-
Clone this repository into your WordPress plugins directory:
cd wp-content/plugins/ -
Navigate to the plugin directory:
cd custom-blocks -
Install dependencies:
npm installIf you encounter dependency conflicts, you can try one of these alternative commands:
npm install --legacy-peer-depsor
npm install --force -
Build the assets:
npm run build -
Activate the plugin through the WordPress admin interface.
-
Build the plugin using the included script:
./build.shOr manually:
npm install --legacy-peer-deps npm run build -
Start the development build with automatic rebuild on file changes:
npm run start -
Make changes to your block files in the
src/blocks/directory.
The plugin uses a custom webpack configuration that ensures proper file organization:
- Each block has its own subdirectory in the build output
- Only the main
index.jsandindex.asset.phpfiles are kept in the root - Block-specific assets are stored in their respective directories
- Shared code chunks are organized in dedicated directories
- All CSS files are properly scoped to their respective blocks
- Each block's
block.jsonfile defines paths to scripts and styles
The final build structure looks like this:
build/
├── index.js # Main entry point that registers all blocks
├── index.asset.php # Asset dependencies for the main file
├── vendors/ # Shared vendor code (if any)
│ └── common/ # Common dependencies
└── blocks/ # Directory containing all blocks
└── sample-block/ # Sample block directory
├── block.json # Block metadata
├── index.js # Block implementation
├── index.css # Editor styles
├── style-index.css # Frontend styles
└── vendors.js # Block-specific dependencies (if any)
The webpack configuration includes several optimizations:
- Smart Code Splitting: Shared dependencies are organized in logical folders
- Whitelist Approach: Only essential files are kept in the root directory
- Block Isolation: Each block's assets are contained in its own directory
- Improved Performance: CSS processing is optimized to reduce duplicated styles
The plugin comes with a sample block implementation that demonstrates key Gutenberg block concepts:
- Rich text editing with the RichText component
- Color settings for background and text
- Padding adjustment with range control
- Basic styling with SCSS
- Block preview in the editor
- Block registration and rendering
To use the sample block:
- Edit any post or page in WordPress
- Click the "+" icon to add a new block
- Search for "Sample Block" and select it
- Use the block inspector panel on the right to customize appearance
To create a new custom block:
-
Duplicate the
sample-blockdirectory withinsrc/blocks/and rename it to your block name (e.g.,my-new-block). -
Edit the files in your new block directory:
block.json: Update the metadata (name, title, description, etc.)edit.js: Customize the editor interface and controlssave.js: Define how the block renders on the frontendstyle.scss: Add frontend stylingeditor.scss: Add editor-specific stylingindex.js: Usually doesn't need changes as it just imports the other files
-
Import your new block in
src/index.js:import "./blocks/sample-block/index.js"; import "./blocks/my-new-block/index.js";
-
Add your block to the main PHP file by adding a new
register_block_typecall in thecustom_blocks_register_blocksfunction:// Register blocks using block.json register_block_type(CUSTOM_BLOCKS_PATH . 'build/blocks/sample-block'); register_block_type(CUSTOM_BLOCKS_PATH . 'build/blocks/my-new-block');
-
Run the build process:
npm run build
For blocks that only require CSS (no JavaScript), you can:
- Register the block in PHP only
- Create a style file for the block
- Enqueue the style file in the main plugin file
For blocks that require server-side rendering:
- Create a callback function in the main plugin file
- Register the block with the
render_callbackparameter - Implement the frontend rendering logic in the callback function
This plugin architecture can be extended to register block patterns:
- Create a new file for registering block patterns
- Define the pattern content and properties
- Include the file in the main plugin file
This plugin is licensed under the GPL v2 or later.