à ¢ Â'ψ ¢ Â' € à ¢ Â' € series
à ¢ Â'‚  à ¢ Â'ψ ¢ Â' € à ¢ Â' € block.js
à ¢ Â'‚  à ¢ Â'ψ ¢ Â' € à ¢ Â' € editor.css
à ¢ Â'‚  à ¢ Â'Â'à ¢ Â' € à ¢ Â' € style.css
à ¢ Â'Â'à ¢ Â' € à ¢ Â' € series.php
Download the main file of the Block in functions.php of the theme:
if (function_exists ('register_block_type')) {
require get_template_directory (). '/blocks/series.php';
}
Note that we attach the download file with one condition: The block is only loaded when Gutenberg is available. This ensures compatibility with previous WordPress versions. The block will now be available in the Gutenberg interface.
This is the interface after inserting Block.
Gutenberg introduced two sets of APIs to register a new Block. If we look at series.php , we will find the following code for registering a new Block. It also loads stylesheets and JavaScripts on the front-end and the editor.
register_block_type ('twentyseventeen / series', array (
'editor_script' => 'series-block-editor',
'editor_style' => 'series-block-editor',
'style' => 'series-block',
));
As we can see above, Block is named twentyseventeen / series, the Block name must be unique so that it does not overlap with the Block that other plugins bring.
Furthermore, Gutenberg provides a new set of JavaScript APIs to interact with the 'Block' interface in the editor. Because API is quite rich, we will focus on some specific details you should know to get a simple but effective Gutenberg block.
First, we will look at wp.blocks.registerBlockType . This function is used to register a new "Block" for the Gutenberg Editor. It requires two arguments. The first argument is that the Block name needs to follow the name registered in the register_block_type function on the PHP side. The second argument is an Object that identifies the Block properties such as title, category and some functions to render the Block interface.
var registerBlockType = wp.blocks.registerBlockType;
registerBlockType ('twentyseventeen / series', {
title: __ ('HTML5 Series'),
category: 'widgets',
keywords: ['html'],
edit: function (props) {},
save: function (props) {}
});
wp.element.createElement
This function allows you to create elements of "Block" in the post editor. The wp.element.createElement function is basically removed from the React createElement () function, so it accepts the same set of arguments. The first argument takes the element type, for example a paragraph, a range, or a div as follows:
wp.element.createElement ('div');
We can put the function into a variable to write shorter. For example:
var el = wp.element.createElement;
el ('div');
If you prefer to use the new ES6 syntax, you can also do it this way:
const {createElement: el} = wp.element;
el ('div');
We can also add element attributes such as class name or id to the second parameter as follows:
var el = wp.element.createElement;
el ('div', {
'class': 'series-html5',
'id': 'series-html-post-id-001'
});
Div created will not make sense without content. We can add content to the argument of the third parameter:
var el = wp.element.createElement;
el ('p', {
'class': 'series-html5',
'id': 'series-html-post-id-001'
}, 'This article is part of our "HTML5 / CSS3 Tutorials series" - dedicated to help you make a better designer and / or developer. Click here to see more articles from the same series');
The wp.components contain a set of names and Gutenberg components. These components are technically React custom components including Button, Popover, Spinner, Tooltip and a host of other components. We can reuse these components into a separate Block. In the following example, we will add a button element.
var Button = wp.components.Button;
el (Button, {
'class': 'download-button',
}, 'Download');
Attributes is a way to store data in Block. This data can be like content, color, sort, URL, etc. We can get attributes from Attributes passed on the edit () function, as follows:
edit: function (props) {
var content = props.attributes.seriesContent;
return el ('div', {
'class': 'series-html5',
'id': 'series-html-post-id-001'
}, Nội dung );
}
To update Attributes, we use the setAttributes () function. Normally we will change the content on certain actions such as when the button is clicked, the input is filled, the option is selected, etc. In the following example, we use it to add backup content of Block in case of an unexpected incident on seriesContent Attribute .
edit: function (props) {
if (typeof props.attributes.seriesContent === 'undefined' ||! props.attributes.seriesContent) {
props.setAttribute ({
seriesContent: 'Hello World! Here is the fallback content. '
})
}
var content = props.attributes.seriesContent;
return [
el ('div', {
'class': 'series-html5',
'id': 'series-html-post-id-001'
}, Nội dung ),
];
}
The save () function works similar to the edit () function, except that it defines the content of Block to save to the database. Saving Block content is quite simple, as we can see below:
save: function (props) {
if (! props ||! props.attributes.seriesContent) {
return;
}
var content = props.attributes.seriesContent;
return [
el ('div', {
'class': 'series-html5',
'id': 'series-html-post-id-001'
}, Nội dung ),
];
}
Gutenberg will change WordPress in a better way (or maybe worse). It allows developers to adopt a new way to develop WordPress plugins and themes. Gutenberg is just a start. Soon, the 'Block' model will be extended to other areas of WordPress such as Settings API and Widget.
Learn JavaScript Deeply is the only way to understand Gutenberg and look forward to the future of WordPress. If you are familiar with the basics of JavaScript, functions, tools, strengths and weaknesses, you will quickly catch up to Gutenberg's speed.
As mentioned, Gutenberg requires a lot of APIs, enough to do almost anything for Block. You can choose to write your Block code with a simple JavaScript, JavaScript with ES6, React syntax, or even Vue.