The do_meta_boxes() WordPress PHP function is used for displaying meta boxes on the screen. It takes in three parameters: a screen identifier, a context for the screen, and an object that’s often the focus of the current screen.
Usage
The function can be used as follows:
do_meta_boxes( 'my_custom_menu_page', 'normal', $post );
In this example, my_custom_menu_page
is the screen identifier, normal
is the context, and $post
is a WP_Post object.
Parameters
- $screen (string|WP_Screen – Required): The screen identifier. If you have used
add_menu_page()
oradd_submenu_page()
to create a new screen, ensure your menu slug conforms to the limits ofsanitize_key()
. - $context (string – Required): The screen context for which to display meta boxes.
- $data_object (mixed – Required): This parameter is passed to the meta box callback function as the first parameter. It’s often the object that’s the focus of the current screen.
More information
See WordPress Developer Resources: do_meta_boxes()
Important to note that the function is not deprecated and can be found in the WordPress source code in the file wp-admin/includes/template.php
.
Examples
Register a new meta box
To register a new meta box, you can use the add_meta_box()
function, and then display it using the do_meta_boxes()
function.
// Adding a custom meta box add_meta_box('meta_box_id', __('Metabox Title'), 'callback_function', 'my_custom_menu_page'); do_meta_boxes('my_custom_menu_page', 'normal', '');
This code registers a new meta box with an ID of meta_box_id
, a title of “Metabox Title”, and a callback function named callback_function
. The do_meta_boxes()
function is then used to display this new meta box.
Re-order meta boxes
You can change the order of meta boxes using the get_user_option_meta-box-order_[CUSTOM_POST_TYPE]
filter.
// Re-order meta boxes add_filter('get_user_option_meta-box-order_post', 'reorder_meta_boxes'); function reorder_meta_boxes($order) { return array( 'normal' => join(',', array('postexcerpt', 'formatdiv', 'trackbacksdiv', 'tagsdiv-post_tag', 'categorydiv', 'postimagediv', 'postcustom', 'commentstatusdiv', 'slugdiv', 'authordiv', 'submitdiv')), 'side' => '', 'advanced' => '', ); }
This example re-orders the meta boxes for the custom post type ‘post’. The order is defined in the array passed to the join()
function.
Displaying a custom meta box
To display a custom meta box, you can use the do_meta_boxes()
function.
// Displaying a custom meta box add_meta_box('my_custom_meta_box', __('My Custom Meta Box'), 'callback_function', 'my_custom_menu_page'); do_meta_boxes('my_custom_menu_page', 'normal', '');
This example displays a custom meta box with the ID my_custom_meta_box
on the custom menu page my_custom_menu_page
.