Function and usage of add_meta_box() in wordpress


Answers ( 1 )


    The add_meta_box() function in WordPress is a powerful tool for developers to add custom meta boxes to various screens in the WordPress admin. These meta boxes can be used for adding custom content or functionality to posts, pages, and custom post types. Here's a detailed explanation of the function and its parameters:


    • Purpose: add_meta_box() adds a custom meta box to one or more screens in the WordPress admin area.
    • Functionality: It allows you to define a new section on the edit screens of posts, pages, or custom post types where you can display custom content or fields.


    1. $id (string, required):

      • Description: This is the unique identifier for the meta box. It's used in the 'id' attribute of the meta box.
      • Example: 'my_meta_box_id'
    2. $title (string, required):

      • Description: The title of your meta box. This will be displayed as the header of the meta box.
      • Example: 'My Custom Meta Box'
    3. $callback (callable, required):

      • Description: A function that outputs the content inside the meta box. This function should echo its output.
      • Example: 'my_meta_box_callback'
      • Callback Function: Typically, you define a separate function that contains the HTML and PHP to output the contents of the meta box.
    4. $screen (string|array|WP_Screen, optional):

      • Description: Determines where on the admin dashboard the meta box will be shown. It can be a post type ('post', 'page', custom post type), 'link', 'comment', or a screen created using add_menu_page() or add_submenu_page().
      • Default: null (current screen)
      • Example: 'post', array('post', 'page')
    5. $context (string, optional):

      • Description: The part of the page where the box will be shown. Common values are 'normal', 'side', and 'advanced'.
      • Default: 'advanced'
      • Example: 'normal'
    6. $priority (string, optional):

      • Description: The priority within the chosen context. Options include 'high', 'core', 'default', 'low'.
      • Default: 'default'
      • Example: 'high'
    7. $callback_args (array, optional):

      • Description: Additional arguments that are passed to the callback function. This can be useful for passing data to the callback.
      • Default: null
      • Example: array( 'arg1' => 'value1', 'arg2' => 'value2' )

    Sample Usage:

    Here's an example of how you might use add_meta_box():

    function my_meta_box_callback( $post, $meta ) {
        // Use $post and $meta['args'] to display custom content here
        echo '<p>Custom content here</p>';
    add_action( 'add_meta_boxes', 'my_add_custom_box' );
    function my_add_custom_box() {
            'my_meta_box_id',          // Unique ID
            'My Custom Meta Box',      // Box title
            'my_meta_box_callback',    // Content callback
            'post',                    // Post type
            'normal',                  // Context
            'high',                    // Priority
            array( 'foo' => 'bar' )    // Callback args

    In this example, my_add_custom_box() is hooked to the add_meta_boxes action. It calls add_meta_box() with specified parameters to create a new meta box. The my_meta_box_callback() function is responsible for rendering the content of the meta box.

Leave an answer