Add Custom Meta Boxes

Looking for adding custom meta boxes through UI? ACF - Advanced Custom Fields

Take a look at the sample code below, you should already be familiar with the `add_meta_boxes` and `save_post` actions.

To enable the metabox for the Post Type Editing screen on the frontend, you can:

  • Replace add_meta_boxes with streamtube_add_meta_boxes

  • Replace add_meta_box with streamtube_add_meta_box

  • Replace 'post_type_name' with 'dashboard_post_type_name'.

In this example, we demonstrate how to register a sample metabox for the Video post type. If you’d like to implement this for a different post type, simply replace 'video' with the desired post type name.

For example:

  • 'post' → 'dashboard_post'

  • 'page' → 'dashboard_page'

  • 'your_post_type' → 'dashboard_your_post_type'


Registering a Custom Metabox

/**
 * Registers a custom meta box for the 'video' post type.
 *
 * This function adds a meta box to the WordPress admin interface for the specified
 * post type. The meta box appears in the 'side' context with a 'high' priority.
 *
 * Meta Box Details:
 * - ID: 'my_meta_box_id'
 * - Title: 'Custom Metabox Title'
 * - Callback: 'my_metabox_callback' (renders the meta box content)
 * - Post Type: 'video' (modifiable)
 * - Context: 'side' (position in the editor)
 * - Priority: 'high' (display priority)
 *
 * @return void
 */
function my_custom_metabox() {
    add_meta_box(
        'my_meta_box_id',         // Unique ID
        'Custom Metabox Title',   // Box title
        'my_metabox_callback',    // Callback function
        'video',                  // Post type (change as needed)
        'side',                   // Context (side, normal, advanced)
        'high'                    // Priority
    );
}
add_action( 'add_meta_boxes', 'my_custom_metabox' );

Rendering the Metabox Content

Saving Metabox Data

Enabling the Metabox for the Frontend

If you want the metabox to appear on the frontend Video Editing screen, modify the `add_meta_boxes` hook to `streamtube_add_meta_boxes` and change `video` to `dashboard_video`:

Done!

Last updated