Adding Custom Meta Boxes to Gutenberg Editor

Posted on 19th June 2023

Adding Custom Meta Boxes to Gutenberg Editor

The Gutenberg editor is a new content editor for WordPress that is currently being developed as part of the core software. It is named after Johannes Gutenberg, who invented the printing press in the 15th century, and is intended to replace the existing TinyMCE-based editor.

One of the goals of the Gutenberg project is to make it easy for developers to create custom blocks for the editor. Blocks are the basic building blocks of the Gutenberg editor, and can be used to create all kinds of content, from simple paragraphs to complex layouts.

In this tutorial, we will show you how to create a custom meta box for the Gutenberg editor. A meta box is a custom field that allows you to add extra data to a post or page. For example, you could use a meta box to add a custom field to a post that stores the author’s Twitter username.

Creating a Meta Box

The first step is to create a new file in your WordPress theme or plugin. We will call ours my-meta-boxes.php.

In this file, we will first need to register our meta box using the register_meta_box() function. This function takes two arguments: the id of the meta box, and the title of the meta box.

We will also need to specify the callback function that will output the content of the meta box. This function will take two arguments: the post object and the meta box arguments.

The post object is a WordPress object that contains all the information about the current post or page. The meta box arguments is an array of arguments that we can use to customize the output of our meta box.

In our callback function, we will use the get_post_meta() function to retrieve the value of our custom field. This function takes three arguments: the post id, the key of the custom field, and a boolean value that specifies whether or not to return a single value or an array of values.

We will then use the update_post_meta() function to save the value of our custom field. This function takes four arguments: the post id, the key of the custom field, the new value of the custom field, and a boolean value that specifies whether or not to overwrite the existing value.

Finally, we will use the delete_post_meta() function to delete the value of our custom field. This function takes three arguments: the post id, the key of the custom field, and a boolean value that specifies whether or not to delete all values of the custom field.

Now that we have registered our meta box, we can output it in the Gutenberg editor by adding the following code to our my-meta-boxes.php file:

function my_meta_boxes_init(){
add_meta_box( ‘my-meta-box’, __( ‘My Meta Box’, ‘my-textdomain’ ), ‘my_meta_box_callback’, ‘post’ ); } add_action( ‘init’, ‘my_meta_boxes_init’ );

This code will add our meta box to the post editor screen in the Gutenberg editor.

In the code above, we have used the add_meta_box() function to register our meta box. This function takes four arguments: the id of the meta box, the title of the meta box, the callback function that will output the content of the meta box, and the post type where the meta box will be displayed.

In our code, we have specified the post type as ‘post’. This will display our meta box on the post editor screen in the Gutenberg editor.

If we wanted to display our meta box on the page editor screen, we would need to change the post type to ‘page’.

Adding Meta Boxes to Custom Post Types

If you want to add meta boxes to a custom post type, you will need to use the add_meta_box() function in the ‘admin_init’ action hook.

This hook is used to add meta boxes to the admin screens of WordPress. It fires after the ‘admin_menu’ hook, which is used to add menus and submenus to the WordPress admin.

In our code, we have used the add_meta_box() function to register our meta box. This function takes four arguments: the id of the meta box, the title of the meta box, the callback function that will output the content of the meta box, and the post type where the meta box will be displayed.

In our code, we have specified the post type as ‘post’. This will display our meta box on the post editor screen in the Gutenberg editor.

If we wanted to display our meta box on the page editor screen, we would need to change the post type to ‘page’.

Adding Meta Boxes to the Block Editor

If you want to add meta boxes to the Block Editor, you will need to use the add_meta_box() function in the ‘init’ action hook.

This hook is used to add meta boxes to the WordPress admin. It fires after the ‘admin_menu’ hook, which is used to add menus and submenus to the WordPress admin.

In our code, we have used the add_meta_box() function to register our meta box. This function takes four arguments: the id of the meta box, the title of the meta box, the callback function that will output the content of the meta box, and the post type where the meta box will be displayed.

In our code, we have specified the post type as ‘post’. This will display our meta box on the post editor screen in the Block Editor.

If we wanted to display our meta box on the page editor screen, we would need to change the post type to ‘page’.

Adding Meta Boxes to the Classic Editor

If you want to add meta boxes to the Classic Editor, you will need to use the add_meta_box() function in the ‘admin_init’ action hook.

This hook is used to add meta boxes to the WordPress admin. It fires after the ‘admin_menu’ hook, which is used to add menus and submenus to the WordPress admin.

In our code, we have used the add_meta_box() function to register our meta box. This function takes four arguments: the id of the meta box, the title of the meta box, the callback function that will output the content of the meta box, and the post type where the meta box will be displayed.

In our code, we have specified the post type as ‘post’. This will display our meta box on the post editor screen in the Classic Editor.

If we wanted to display our meta box on the page editor screen, we would need to change the post type to ‘page’.

Conclusion

In this tutorial, we have shown you how to create a custom meta box for the Gutenberg editor. We have also shown you how to add meta boxes to the Block Editor and the Classic Editor.

If you have any questions, please leave a comment below.