Sunday, January 19, 2025
HomeEveryday WordPressHow to build a Gutenberg plugin to extend editor functionality

How to build a Gutenberg plugin to extend editor functionality


Over the years, we have explored Gutenberg from many perspectives. We have dissected the editor’s functionalities, compared them with those of other page builders, built static and dynamic custom blocks, and much more.

With the latest versions of WordPress, new features and tools allow you to create complex layouts more easily by enriching and enhancing the functionality of the block editor without the need to build custom blocks.

Thanks to the introduction of block patterns and developer tools such as the Block Bindings API, there are fewer use cases for custom blocks. You can now create complex block structures, inject metadata values into your content, and automate a good part of your workflow without leaving the editor’s interface. In short, today, WordPress allows you to create complex sites with as few clicks as ever before.

Adding custom controls and tools to the editor may help make the content creation process smoother. For example, you may need to add a panel to the Post sidebar to add functionality or create a custom sidebar to manage multiple meta fields.

Let’s get started!

How to create a block editor plugin without creating a custom block

WordPress provides a convenient command-line tool that allows you to install a local Node.js development environment with all the necessary files and dependencies to create a custom block. Just type npx @wordpress/create-block in the command line tool, wait a few seconds, and you are done.

However, scaffolding a custom block is not necessary when you only need to add a sidebar or a simple settings panel. In this case, you need to create a Gutenberg plugin.

WordPress does not provide a utility to automate the process of creating a plugin for Gutenberg, so you need to do it manually. But do not worry too much. The process is relatively straightforward, and we will drive you through it. These are the steps to follow:

1. Download and install a local development environment

First things first: Although you can develop your Gutenberg plugin in a remote environment, it may be more convenient for you to install a development WordPress website locally. You can use any environment based on PHP and MySQL. Among the many alternatives available out there, we recommend DevKinsta. It’s free, fully featured, easy to use, and 100% compatible with Kinsta hosting.

Once you have your development site set up, you are ready to create a Gutenberg block editor plugin.

2. Download and install Node.js and npm

Download Node.js from nodejs.org and install it on your computer. This will also install npm, the Node package manager.

Once you’ve done this, launch your command-line tool and run node -v and npm -v. You should see the installed versions of Node.js and npm.

Check node and npm versions

3. Create your plugin’s folder

Create a new folder under wp-content/plugins and rename it my-sidebar-plugin or something similar. Just remember that this name should reflect your plugin’s name.

Open the terminal, navigate to the plugin’s folder, and initialize an npm project with the following command:

npm init -y

This will create a basic package.json file.

Create a basic package.json file
Create a basic package.json file

4. Install dependencies

In your command-line tool, type the following command:

npm install @wordpress/plugins @wordpress/scripts --save-dev

A new node_modules folder should have been added to your project.

Now, open your package.json and update the scripts as follows:

{
	"name": "my-sidebar-plugin",
	"version": "1.0.0",
	"main": "index.js",
	"scripts": {
		"build": "wp-scripts build",
		"start": "wp-scripts start"
	},
	"keywords": [],
	"author": "",
	"license": "ISC",
	"description": "",
	"devDependencies": {
		"@wordpress/plugins": "^7.14.0",
		"@wordpress/scripts": "^30.7.0"
	}
}

Now you can check the plugin’s folder:

The plugin's project in Visual Studio Code
The plugin’s project in Visual Studio Code

5. Create the plugin’s files

In your plugin’s directory, create a new .php file and give it the same name as your folder. In our example, it is my-sidebar-plugin.php.

Open the file and paste the following code to register the plugin on the server:

Next, create a src folder in your plugin’s directory. In there, create a new index.js file with the following code:

import { registerPlugin } from '@wordpress/plugins';
import { PluginSidebar } from '@wordpress/edit-post';

const MyPluginSidebar = () => (
	
		

Hello my friends!

); registerPlugin( 'my-sidebar-plugin', { render: MyPluginSidebar, } );


Source link
RELATED ARTICLES
Continue to the category

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Most Popular

Recent Comments