The add_meta_box() is a WordPress builtin function which adds a meta box to one or more screens.
Syntax:
1 2 3 | 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 ) |
1. $id: (string) (Required) Meta box ID (used in the ‘id’ attribute for the meta box).
2. $title: (string) (Required) Title of the meta box.
3. $callback: (callable) (Required) Function that fills the box with the desired content. The function should echo its output.
4. $screen: (string|array|WP_Screen) (Optional) The screen or screens on which to show the box (such as a post type, ‘link’, or ‘comment’).
5. $context: string) (Optional) The context within the screen where the boxes should display. Available contexts vary from screen to screen.
6. $priority: (string) (Optional) The priority within the context where the boxes should show (‘high’, ‘low’). Default value: ‘default’
7. $callback_args: (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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | /** * This function adds a meta box with a callback function of my_metabox_callback() */ function add_wpdocs_meta_box() { $var1 = 'this'; $var2 = 'that'; add_meta_box( 'metabox_id', __( 'Metabox Title', 'textdomain' ), 'wpdocs_metabox_callback', 'page', 'normal', 'low', array( 'foo' => $var1, 'bar' => $var2 ) ); } /** * Get post meta in a callback * * @param WP_Post $post The current post. * @param array $metabox With metabox id, title, callback, and args elements. */ function wpdocs_metabox_callback( $post, $metabox ) { // Output last time the post was modified. echo 'Last Modified: ' . $post->post_modified; // Output 'this'. echo $metabox['args']['foo']; // Output 'that'. echo $metabox['args']['bar']; // Output value of custom field. echo get_post_meta( $post->ID, 'wpdocs_custom_field', true ); } |
An often forgotten, but also very important, fact is that any save_post handler should check for a multisite switched context. Here’s an example of such guard:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | namespace DevWpNote\MetaBox; add_action( 'save_post', __NAMESPACE_ . '\save_fields', 10, 3 ); function save_fields( $post_id, WP_Post $post, $update ) { // check nonce ... // check autosave ... // check user capabilities ... // check if there was a multisite switch before if ( is_multisite() && ms_is_switched() ) { return $post_id; } // handle your meta box input ... } |
This is the way to register menu screen metabox.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function op_register_menu_meta_box() { add_meta_box( 'op-menu-meta-box-id', esc_html__( 'Op Menu MetaBox Title', 'text-domain' ), 'op_render_menu_meta_box', 'nav-menus', 'side', 'core' ); } add_action( 'load-nav-menus.php', 'op_register_menu_meta_box' ); function op_render_menu_meta_box() { // Metabox content echo '<strong>Hi, I am MetaBox.</strong>'; } |
Reference: https://developer.wordpress.org/reference/functions/add_meta_box/
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.