WordPress add_meta_box() Function

The add_meta_box() is a WordPress builtin function which adds a meta box to one or more screens.



1. $id: (string) (Required) Meta box ID (used in the ‘id’ attribute for the meta box).
2. $title: (string) (Required) Title of the meta box.
3. $callback: (callable) (Required) Function that fills the box with the desired content. The function should echo its output.
4. $screen: (string|array|WP_Screen) (Optional) The screen or screens on which to show the box (such as a post type, ‘link’, or ‘comment’).
5. $context: string) (Optional) The context within the screen where the boxes should display. Available contexts vary from screen to screen.
6. $priority: (string) (Optional) The priority within the context where the boxes should show (‘high’, ‘low’). Default value: ‘default’
7. $callback_args: (array) (Optional) Data that should be set as the $args property of the box array (which is the second parameter passed to your callback). Default value: null

WordPress add_meta_box() Function Example #1

Example #2

An often forgotten, but also very important, fact is that any save_post handler should check for a multisite switched context. Here’s an example of such guard:

Example #3

This is the way to register menu screen metabox.

Reference: https://developer.wordpress.org/reference/functions/add_meta_box/

If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.

Recommended Posts: