Till the time WordPress came into existence it’s visual editor remained pretty unchanged and that’s not a bad thing, but other platforms like Medium got popular in between and they are providing a unique layout for writing and expressing your thoughts. So, developers, contributors, and volunteers came together and started working on the new rich content editor called Gutenberg.
Let’s go through a ride to know what exactly Gutenberg is and how one can use it and can help in its betterment.
What is Gutenberg?
If you exactly wanted to see how it looks like and how a writer can use it just look at this link. You will feel like you are using some builder tool to develop and design a page and that’s what it is all about – A fantastic new post/page editor 😎
It’s basically named after Johannes Gutenberg, who introduced printing to Europe with the printing press. In the new editor, you will see that there are multiple blocks that you can use to create the page or post which was initially not possible in such manner without any builder tool like Visual Composer or Elementor.
Even developers were needed to create shortcode or HTML to get such kind of view on the page and that’s now possible to do with the editor itself – Awesome, Right?
Gutenberg is still in testing phase so it should not be used on live site directly but can be tested on the staging environment. Since it is still in testing phase so it requires people to install the plugin and let the development team know whether it is good or not.
Note: Anyone who wants to leave feedback can leave comments here or can open an issue on GitHub and can also join the #core-editor channel.
Most importantly, the current version of Gutenberg can be tested with the WordPress version 4.8 and it will become the default editor from WordPress 5.0 but you will still have the option to install the classic editor.
Installing Gutenberg
Please download the latest version of Gutenberg from the WordPress repository or by searching for it within your WordPress dashboard under “Add New” plugins. As suggested it is still not ready for the live environment so please do activate on test site/ staging site only.
Once you will activate it you will see a new link under your posts named gutenberg that allows opening the Gutenberg editor. And rest of the layout of the editor you have already seen here. You can play with the editor more to explore more and you will feel really blessed that you saw this change in WordPress 😆
Let’s see how it looks like while editing the post/page.
What’s there for developers?
Since this new editor is all about blocks and is based on react js there is a lot to do for developers. So guys and girls WordPress itself have a wonderful handbook which you can refer to.
Let’s see how one can create its own block as you saw in the demo. So we are going to start with a simple example of a new block type which displays a styled message in a post.
Blocks containing static content are implemented entirely in JavaScript using the registerBlockType function. This function specifies the structure of the block and describes the behavior to let the editor know that how it will appear.
Enqueuing Block Scripts
While the block’s editor behaviors are implemented in JavaScript, you’ll need to register your block server-side to ensure that the script is enqueued when the editor loads.
function gutenberg_para_block() {
wp_register_script(
'gutenberg-para-script',
plugins_url( 'para/block.js', __FILE__ ),
array( 'wp-blocks', 'wp-element' )
);
register_block_type( 'gutenberg-para-block/hello-gutenberg-para', array(
'editor_script' => 'gutenberg-para-script',
) );
}
add_action( 'init', 'gutenberg_para_block' );
You can see these two ‘wp-blocks’, ‘wp-element’ script dependencies:
wp-blocksincludes block type registration and related functionswp-elementincludes the WordPress Element abstraction for describing the structure of your blocks
Registering the Block
So now since the script is included, let’s see the implementation.
var el = wp.element.createElement,
registerBlockType = wp.blocks.registerBlockType,
blockStyle = { backgroundColor: '#900', color: '#fff', padding: '20px' };
registerBlockType( 'gutenberg-para-block/hello-gutenberg-para', {
title: 'Hello Gutenberg',
icon: 'universal-access-alt',
category: 'layout',
edit: function() {
return el( 'p', { style: blockStyle }, 'Hello editor.' );
},
save: function() {
return el( 'p', { style: blockStyle }, 'Hello saved content.' );
},
} );
Now, you will see that block is listed in the editor. Is’nt it simple?
References
I know there is a lot to be told and a lot to be known so if you have any queries or feedback please do let me know.
Thanks for reading 🙂