Using WordPress ‘do_meta_boxes’ PHP action

The do_meta_boxes WordPress PHP action fires once for each of the default meta box contexts: normal, advanced, and side.

Usage

add_action('do_meta_boxes', 'your_custom_function', 10, 3);

function your_custom_function($post_type, $context, $post) {
    // your custom code here
}

Parameters

  • $post_type: (string) Post type of the post on Edit Post screen, ‘link’ on Edit Link screen, ‘dashboard’ on Dashboard screen.
  • $context: (string) Meta box context. Possible values include ‘normal’, ‘advanced’, ‘side’.
  • $post: (WP_Post|object|string) Post object on Edit Post screen, link object on Edit Link screen, an empty string on Dashboard screen.

More information

See WordPress Developer Resources: do_meta_boxes

Examples

Display a message after meta boxes

add_action('do_meta_boxes', 'display_message_after_meta_boxes', 10, 3);

function display_message_after_meta_boxes($post_type, $context, $post) {
    if ($context == 'side') {
        echo "**This is a side meta box.**";
    }
}

Add custom CSS for specific post type

add_action('do_meta_boxes', 'add_custom_css_for_post_type', 10, 3);

function add_custom_css_for_post_type($post_type, $context, $post) {
    if ($post_type == 'your_post_type') {
        echo "<style>.your-custom-class { color: red; }</style>";
    }
}

Display context-specific message

add_action('do_meta_boxes', 'display_context_specific_message', 10, 3);

function display_context_specific_message($post_type, $context, $post) {
    if ($context == 'advanced') {
        echo "**Welcome to the advanced context.**";
    }
}

Remove specific meta box from dashboard

add_action('do_meta_boxes', 'remove_specific_meta_box_from_dashboard', 10, 3);

function remove_specific_meta_box_from_dashboard($post_type, $context, $post) {
    if ($post_type == 'dashboard') {
        remove_meta_box('your_meta_box_id', 'dashboard', $context);
    }
}

Add custom content after ‘normal’ context meta boxes

add_action('do_meta_boxes', 'add_custom_content_after_normal_context', 10, 3);

function add_custom_content_after_normal_context($post_type, $context, $post) {
    if ($context == 'normal') {
        echo "**This is a custom message after normal context meta boxes.**";
    }
}

Tagged in

Leave a Comment

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