If you want to make the plug-in compatible with Gutenberg, there are several ways you can or should take according to the operation performed by the plug-in and the way the plug-in provides the function to the user. In the first part of this mini series, we looked at the gutenberk block API that most plug-ins require for gutenberk compatibility. This time, let’s take a look at another Gutenberg sidebar API of the Gutenberg puzzle. To ensure that we are on the same page, simply explain that Gutenberg’s block API is very extensive. It can build almost all content into blocks, but sometimes it is not enough. In contrast, the sidebar API allows plug-ins to register sidebars that can use functionality across blocks. The sidebar is similar to the block inspector. Male
How to make \wordpress plug-in compatible with \gutenberg – sidebar mastering \api twitter. For example, the Gutenberg sidebar API demonstrated in yoast SEO: where can I use the sidebar API? We recently provided Gutenberg with a plug-in WP product review. WP product review uses the meta field to add comment data to the post. Due to compatibility with various previous versions, it cannot switch to the block. So I used the sidebar API. The following are the plug-in blocks in the classic editor: The method of integrating WP product review into Gutenberg editor is as follows: Users can configure all contents of the comment box in the sidebar option if the post is displayed as a comment with sidebar switching. The
WP product review is one of the examples where the sidebar API may be useful. Another good example is the drop it plug-in, where users can use the sidebar to insert library images of unsplash and giphy. This post will guide you through similar functionality and make it compatible with the plug-in Gutenberg through the sidebar API. By default, the top move metabox is somewhat ready for Gutenberg, but it is not complete. Let’s start with metabox. In the editor screen, let’s look at a plug-in that uses a simple meta box to provide some functionality to the user. The
In the traditional editor, it can be as follows: You can use the following code to create these meta boxes. It can also be used in the Hello Gutenberg repository\/**
*Sign up for Hello gutenbert metabox
*\/The
Function hello\u gutenberg\u add\u meta\u box(){
Add\u meta\u box (\
}The
Add\u action (‘add\u meta\u boxes’,’hello\u gutenberg\u add\u meta\u box’);
\/**The
*Hello Gutenberg metabox callback
*\/The
Function hello\u gutenberg\u metabox\u callback ($post){
$value = get\u post\u meta ($post->id,’\u hello\u gutenberg\u field’, true);
? >
Male male
Male \
Male lies,
));
}The
Add\u action (‘add\u meta\u boxes’,’hello\u gutenberg\u add\u meta\u box’); Gutenberg Handbook provides details on how to port PHP metaboxes to Gutenberg (here). The
In this way, the old PHP metabox will no longer appear in Gutenberg. Now, now
More components are required. You can change the top half of the code to\/**
*Internal block library
*\/The
Constant {\\\\\\\\\\\\\\\\\\wp. I18N;
Constant{
Plug in sidebar
Plug in sidebar moremenuitem
}= wp Editpost;
Constant{
Panel body
Text control
}= wp Components;
Constant{
Elements
debris
}= wp element
Const {withselect} = wp Data;
Const{register plugin}=wp. Plugins; Let’s take a quick look at all the imported components. The
As discussed in the previous article, \u components are used to make text translatable. The pluginsidebar component is used to add a sidebar to Gutenberg. Pluginsidebarmoremenuitem is used to add a button to the Gutenberg menu to toggle the visibility of the sidebar. The panelbody component is used to add panels to the sidebar. Textcontrol is used for input fields. Then the component and fragment components are imported. Both are realt components used by the sidebar. The fragment component is used to group child lists instead of adding other nodes to the dom. JSX cannot have multiple parent nodes, so you must use the fragment component in your code. Withselect is a high-level component. It is recommended that you read this document to understand what high-level components are. Finally, get a registerplugin similar to the registerblock type used in the previous article. Registerplugin registers plug-ins, not blocks. Before adding a control, the sidebar is only a component function, but the lifecycle method of react will be used, so it will be converted to react components: Class Hello \u Gutenberg extension component{
Render (){
Returns(
Male
Male
{\u (\
Male
Male
Male {\u (‘Hello world! ‘)} male
Male
Male
)The
}The
}The
Registerplugin (‘Hello gutenberg'{
Icon: \
Rendering: Hello \u Gutenberg,
}); This will make the sidebar work as plain text. Now let’s add fields to the sidebar. Components must be displayed as follows: Class Hello \u Gutenberg extension component{
Render (){
Returns(
Male
Male
{\u (\
Male
Male
Male
Male
Male
Male
Male
)The
}The
}A simple input field is added to the sidebar, which does nothing. There are two things left. Displays the value of the meta field. The sidebar allows you to update meta field values. Display metadata value to get metadata WP. Apifetch will be used. Apifetch is a utility library that allows you to make rest API requests. Use apifetch in the creator of the react component: Class Hello \u Gutenberg extension component{
Created by (){
Super (… Acquisition);
This State ={
Key: \
Value: ”,
}The
Wp Apifetch ({path: `\/wp\/v2\/posts\/${this.props.postid}`, method: \
(data) = >{
This Setstate ({
Value: data. Meta_ Hello\u gutenberg\u fiel
D
});
Return data
},
(error) = >{
Return error
}The
);
}The
Render (){
Returns(
Male
Male
{\u (\
Male
Male
Male
Male
Male
Male
Male
)The
}The
}First, we define the initial state, which is the key and value of the meta field by default. Then use apifetch to get data from the current post$ The {this.props.postid} variable is passing the ID of the current post. This point will be discussed later. After importing the data, update the status to the value of the rest API. Now go back to the postid variable. Because you don’t know the ID of the current post, please use the withselect high-level component: Const hoc=withselect (= >{
Const{getcurrentpostid}=select (\
Returns
Postid: getcurrentpostid(),
};
}) (hello_gutenberg);
Registerplugin (‘Hello gutenberg'{
Icon: \
Rendering: hoc,
}); It passes the ID of the current post as a postid variable. Now you can run the previous post, and the Gutenberg sidebar shows the meta field value. Updating metadata values must now allow the sidebar to update metadata values. WP is similar to getting details. Use the apirequest utility. Update the status value to the rest API whenever it changes. To do this, you must first add the onchange event to textcontrol, as follows: Male{
This Setstate ({
price
});
}}
\/>The rest request is then sent using the getderive dstatefromprops lifecycle method. You can add the following code under the Creator: Static getderive dstatefromprops (nextprops, state){
Wp Apirequest ({path: \/\/hello gutenberg\/v1\/update meta? Id=${nextprops.postid}`, method: \
(data) = >{
Return data
},
(error) = >{
Return error
}The
);
}The meta field is updated each time you make changes in the field. Now you must confirm that there is a problem with this method. The meta value is updated every time the meta value is changed. This method has two problems. Even if you decide not to update the post, the data will be saved. HTTP calls are too frequent and not ideal. Data can only be saved when saving or publishing a post. To do this, you can use the high-level component to view the save time of the post. You can replace the hoc function with the following code: Const hoc=withselect ((optional, {forceissaving}) = >{
Constant{
Getcurrentpostid,
Issavingpost,
Ispublishing post,
Isautosavingpost,
}=Select (\
Returns
Postid: getcurrentpostid(),
Issaving: forceissaving | issavingpost(),
Isautosaving: isautosavingpost(),
Ispublishing: ispublishing post(),
};
}) (hello_gutenberg); This will provide several variables to verify whether the post is saved or posted. This condition must be added to the feature after the request. Static getderived
Statefromprops (nextprops, status){
If ((nextprops.ispublishing | nextprops.issaving) & &! Nextprops Isautosaving){
Wp Apirequest ({path: \/\/hello gutenberg\/v1\/update meta? Id=${nextprops.postid}`, method: \
(data) = >{
Return data
},
(error) = >{
Return error
}The
);
}The
}Now, metadata values are updated only when a post is saved, updated, or published. That’s all! Now that the sidebar is complete, you have learned how to make the plug-in compatible with Gutenberg! You can test the block and verify that it works. Otherwise, please respond in the comments section below and we will try our best to help you. What is the next step after the top mobile sidebar API is completed? The completed example can be found in the Hello Gutenberg repository. If you find a problem, please open it freely. Again, this is a possible small example of Gutenberg’s sidebar API. Gutenberg and its API have a lot of amazing things. Additional reading: how to use Gutenberg tutorial new WordPress Block Editor for novice developers – WordPress Gutenberg guide Gutenberg component create \