Metabox Tutorial – add_meta_box() Function

add_meta_box() function is a wordpress function. add_meta_box function is used to create custom meta boxes. This function is only responsible for registering and displaying custom meta boxes.


add_meta_box( string $id, string $title, callable $callback, string|array|WP_Screen $screen = null, string $context = 'advanced', string $priority = 'default', array $callback_args = null )

Parameters details

(string) (Required) Meta box ID (used in the ‘id’ attribute for the meta box).

(string) (Required) Title of the meta box.

(callable) (Required) Function that fills the box with the desired content. The function should echo its output.

(string|array|WP_Screen) (Optional) The screen or screens on which to show the box (such as a post type, ‘link’, or ‘comment’). Accepts a single screen ID, WP_Screen object, or array of screen IDs. Default is the current screen. If you have used add_menu_page() or add_submenu_page() to create a new screen (and hence screen_id), make sure your menu slug conforms to the limits of sanitize_key() otherwise the ‘screen’ menu may not correctly render on your page.

Default value: null

(string) (Optional) The context within the screen where the box should display. Available contexts vary from screen to screen. Post edit screen contexts include ‘normal’, ‘side’, and ‘advanced’. Comments screen contexts include ‘normal’ and ‘side’. Menus meta boxes (accordion sections) all use the ‘side’ context. Global

Default value: ‘advanced’

(string) (Optional) The priority within the context where the box should show. Accepts ‘high’, ‘core’, ‘default’, or ‘low’.

Default value: ‘default’

(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

We can find this function inside File: wp-admin/includes/template.php


 * Register meta box(es).
function owt_register_post_meta_box()
    add_meta_box('meta-box-id', __('My Meta Box', 'textdomain'), 'owt_my_display_callback', 'post');
add_action('add_meta_boxes', 'owt_register_post_meta_box');

 * Custom metabox callback function 
function owt_my_display_callback()
    echo "This is sample metabox";

Above code adds a metabox inside Create Post / Edit Post page. You can add this code into your custom plugin code or add inside activated theme functions.php file.

Still we have a sample message into it. We will add inputs in coming tutorials.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more