Packaging & Publishing Blocks
Distribution Options
| Method | Best For |
|---|---|
| WordPress Plugin | General distribution, WordPress.org |
| npm Package | Developers, multiple projects |
| Theme Bundle | Theme-specific blocks |
| Block Directory | Single-purpose blocks |
Building for Production
# Development (watch mode)
npm start
# Production build
npm run build
# Create plugin zip
npm run plugin-zipThe build/ folder contains optimized, minified assets.
Plugin Structure
my-blocks-plugin/
โโโ build/
โ โโโ blocks/
โ โ โโโ testimonial/
โ โ โ โโโ block.json
โ โ โ โโโ index.js
โ โ โ โโโ style-index.css
โ โ โ โโโ render.php
โ โ โโโ pricing-table/
โ โ โโโ ...
โ โโโ index.js
โโโ src/
โ โโโ blocks/
โ โโโ testimonial/
โ โโโ pricing-table/
โโโ my-blocks-plugin.php
โโโ package.json
โโโ readme.txt
โโโ webpack.config.jsMain Plugin File
<?php
/**
* Plugin Name: My Blocks Collection
* Description: Custom Gutenberg blocks.
* Version: 1.0.0
* Requires at least: 6.3
* Requires PHP: 8.0
* Author: Your Name
* License: GPL-2.0-or-later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
function my_blocks_register() {
// Register all blocks in the build folder
$blocks_dir = __DIR__ . '/build/blocks/';
if ( ! is_dir( $blocks_dir ) ) {
return;
}
$block_folders = array_diff( scandir( $blocks_dir ), array( '.', '..' ) );
foreach ( $block_folders as $block ) {
$block_path = $blocks_dir . $block;
if ( is_dir( $block_path ) && file_exists( $block_path . '/block.json' ) ) {
register_block_type( $block_path );
}
}
}
add_action( 'init', 'my_blocks_register' );
// Register custom block category
function my_blocks_categories( $categories ) {
return array_merge(
array(
array(
'slug' => 'my-blocks',
'title' => __( 'My Blocks', 'my-blocks' ),
),
),
$categories
);
}
add_filter( 'block_categories_all', 'my_blocks_categories' );Multiple Blocks Setup
For multiple blocks, configure webpack:
// webpack.config.js
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const path = require( 'path' );
module.exports = {
...defaultConfig,
entry: {
'blocks/testimonial/index': './src/blocks/testimonial/index.js',
'blocks/pricing-table/index': './src/blocks/pricing-table/index.js',
'blocks/posts-grid/index': './src/blocks/posts-grid/index.js',
},
output: {
path: path.resolve( __dirname, 'build' ),
filename: '[name].js',
},
};Sharing Styles with theme.json
Blocks can integrate with theme.json:
// In your theme's theme.json
{
"settings": {
"custom": {
"testimonial": {
"backgroundColor": "#f8f9fa",
"borderRadius": "8px"
}
}
}
}// In block styles
.wp-block-theme-testimonial {
background: var(--wp--custom--testimonial--background-color);
border-radius: var(--wp--custom--testimonial--border-radius);
}Publishing to npm
For sharing with developers:
// package.json
{
"name": "@yourname/wordpress-blocks",
"version": "1.0.0",
"main": "build/index.js",
"files": [
"build/",
"src/"
],
"publishConfig": {
"access": "public"
}
}npm login
npm publish --access publicBlock Directory Submission
For single-purpose blocks on WordPress.org Block Directory:
- Follow WordPress.org plugin guidelines
- Include only ONE block per submission
- Must work without configuration
- Submit at wordpress.org/plugins/developers/add/
readme.txt for WordPress.org
=== My Testimonial Block ===
Contributors: yourwpusername
Tags: block, testimonial, reviews, gutenberg
Requires at least: 6.3
Tested up to: 6.4
Stable tag: 1.0.0
License: GPLv2 or later
A customizable testimonial block with ratings and avatars.
== Description ==
Create beautiful testimonials with:
* Star ratings (1-5)
* Customer avatars
* Multiple layout styles
== Installation ==
1. Upload the plugin files to `/wp-content/plugins/my-testimonial-block`
2. Activate the plugin
3. Use the block in the editor!
== Changelog ==
= 1.0.0 =
* Initial releaseBlocks in Themes
Include blocks directly in your theme:
// In functions.php
function theme_register_blocks() {
register_block_type( get_template_directory() . '/blocks/hero/block.json' );
}
add_action( 'init', 'theme_register_blocks' );Congratulations!
You've completed the Custom Gutenberg Blocks tutorial!
What You've Learned
- โ Block architecture and development setup
- โ Building Edit components with React
- โ Save components and serialization
- โ Inspector Controls and Block Toolbar
- โ Dynamic blocks with PHP rendering
- โ Block variations and transforms
- โ Frontend interactivity
- โ Packaging and distribution
Next Steps
- Build a complete block plugin with 3-5 blocks
- Explore InnerBlocks for nested content
- Learn the Query Loop block patterns
- Contribute to WordPress core blocks
๐ฏ Tutorial Complete! You're now ready to build professional Gutenberg blocks.