WordPress add_meta_box() Function

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

Syntax:

Parameters

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:


Prem Tiwari

An engineer by profession and a passionate blogger by heart. Founder of FreeWebMentor.com (A Programming blog for beginners), Tech Speaker at various forums. A part from this he is an open source enthusiast, WordPress Lover, Blogger, SEO, and Growth Strategic.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.