The editor will endeavor to create a new page and post building experience that makes writing rich posts effortless, and has “blocks” to make it easy what today might take shortcodes, custom HTML, or “mystery meat” embed discovery.
Note
The WordPress Editor
The Classic Editor Plugin
Using Blocks for Content and Design
Using Blocks for Functionality
Creating Your Own Blocks
Minimal Block Example
wp
.
blocks
.
registerBlockType
(
'bwawwp/minimal'
,
{
title
:
'Minimal Block Example'
,
category
:
'common'
,
edit
()
{
return
'Minimal block editor content.'
;
},
save
()
{
return
'Minimal block frontend content.'
;
},
}
);
block.js
minimal-block-example.php
/**
* Plugin Name: Minimal Block Example
*/
function
enqueue_min_block
()
{
wp_enqueue_script
(
'minimal-block'
,
plugins_url
(
'block.js'
,
__FILE__
),
array
(
'wp-blocks'
)
);
}
add_action
(
'enqueue_block_editor_assets'
,
'enqueue_min_block'
);
Note
Using Custom Blocks to Build App Experiences
Enabling the Block Editor in Your CPTs
register_post_type
(
'homework'
,
array
(
'labels'
=>
array
(
'name'
=>
__
(
'Homework'
),
'singular_name'
=>
__
(
'Homework'
)
),
'public'
=>
true
,
'has_archive'
=>
true
,
'supports'
=>
array
(
'title'
,
'editor'
),
// New
'show_in_rest'
=>
true
,
// New
)
);
Block Categories
// Add a Homework block category.
function
my_block_categories
(
$categories
,
$post
)
{
return
array_merge
(
$categories
,
array
(
array
(
'slug'
=>
'homework'
,
'title'
=>
'Homework'
,
),
)
);
}
add_filter
(
'block_categories'
,
'my_block_categories'
),
10
,
2
);
The Homework Blocks
Limiting Blocks to Specific CPTs
// Deregister instructions on non-homework posts.
wp
.
domReady
(
function
()
{
if
(
wp
.
data
.
select
(
'core/editor'
).
getCurrentPostType
()
!=
'homework'
)
{
wp
.
blocks
.
unregisterBlockType
(
'homework/instructions'
);
}
});
Limiting CPTs to Specific Blocks
// Only allow certain blocks on homework posts.
function
my_allowed_block_types
(
$allowed_blocks
,
$post
)
{
if
(
$post
->
post_type
==
'homework'
)
{
$allowed_blocks
=
array
(
'core/block'
,
'core/image'
,
'core/paragraph'
,
'core/heading'
,
'core/list'
,
'homework/instructions'
,
'homework/question'
,
);
}
return
$allowed_blocks
;
}
add_filter
(
'allowed_block_types'
,
'my_allowed_block_types'
,
10
,
2
);
Block Templates
// Register the homework CPT.
register_homework_post_type
(
'homework'
,
array
(
'labels'
=>
array
(
'name'
=>
__
(
'Homework'
),
'singular_name'
=>
__
(
'Homework'
)
),
'public'
=>
true
,
'has_archive'
=>
true
,
'supports'
=>
array
(
'title'
,
'editor'
),
'show_in_rest'
=>
true
,
'template'
=>
array
(
array
(
'homework/instructions'
),
array
(
'homework/question'
,
array
(
'content'
=>
'True/false 1.'
,
'question_type'
=>
'true_false'
)
),
array
(
'homework/question'
,
array
(
'content'
=>
'Essay question.'
,
'question_type'
=>
'essay'
)
),
)
)
);
Saving Block Data to Post Meta
<!-- wp:homework/instructions -->
<p
class=
"wp-block-homework-instructions-content"
>
Email me your answers.</p>
<p
class=
"wp-block-homework-instructions-due_date"
>
Due Date: 2020-11-01</p>
<!-- /wp:homework/instructions -->
register_post_meta
(
'homework'
,
'_homework_due_date'
,
array
(
'show_in_rest'
=>
true
,
'single'
=>
true
,
'type'
=>
'string'
,
'auth_callback'
=>
function
()
{
return
current_user_can
(
'edit_posts'
);
}
)
);
wp
.
blocks
.
registerBlockType
(
'homework/instructions'
,
{
// ...
attributes
:
{
content
:
{
type
:
'array'
,
source
:
'children'
,
selector
:
'p'
,
},
due_date
:
{
type
:
'string'
,
meta
:
'_homework_due_date'
,
source
:
'meta'
,
default
:
''
,
}
},
// ...
}
Tips
Enable WP_SCRIPT_DEBUG
Use filemtime() for the Script Version
wp_register_script
(
'homework-question'
,
BWAWWP_URL
.
'homework-cpt/blocks/question/blocks.js'
,
array
(
'wp-blocks'
,
'wp-element'
,
'wp-editor'
,
'wp-components'
,
'wp-dom-ready'
,
'wp-edit-post'
,
),
filemtime
(
BWAWWP_DIR
.
'homework-cpt/blocks/question/blocks.js'
)
);
More Tips
Learn JavaScript, Node.js, and React More Deeply
- JavaScript
We cover JavaScript and its history with WordPress in This Blog. The stronger your JavaScript skills, the easier you will find it to develop blocks. JavaScript: The Definitive Guide, by David Flanagan, and JavaScript: The Good Parts, by Douglas Crockford, are two great books for learning JavaScript. You can browse them to pick up the basics and use them for reference or dive into them to “learn JavaScript deeply.”
- Node.js*
Node.js is a JavaScript runtime that will run in your server environment. Several Node.js-based tools are used in the typical Gutenberg build setup, including webpack, Babel, and the Node Package Manager (npm). Webpack is used to compile all your JavaScript into a single bundle to be deployed to the browser. Babel is used to convert ESNext and JSX code into JavaScript that will run across the widest range of browsers. The npm is used to install JavaScript packages and manage the dependencies between them. Both webpack and Babel are typically installed as node modules. You may also want to use additional node libraries in the JavaScript code for your blocks. You can install and manage those via npm. The “JavaScript Build Setup” section of the Block Editor Handbook will walk you through installing node and npm, then installing the @wordpress/scripts package, which includes the recommended default configurations for webpack and Babel.
- React
We give a full description of React in This Blog. In summary, React is a JavaScript library for building interactive UIs that is used extensively in the block editor screens. A WordPress block can be viewed as a collection of React UI components designed to serve a common editor or content style use case. WordPress has its own library of React components specific for building blocks. These can be combined with other React components or custom developed components. Experimenting with React outside the context of building a Gutenberg block will help you to get familiar with React in general. When you run into issues building WordPress blocks, you will be better equipped to know if those issues are React issues or WordPress issues, which will help you to know where to go for help. If you’d like to dive deep into React, check out the book Learning React, by Alex Banks and Eve Porcello. Just going through the official tutorial on the React website or one of the other examples there will be very useful as well.
1 A full list of formatting shortcuts and other useful shortcut keys can be found on the WordPress site.