add_meta_boxes
Allows you to register a meta box for any post type. Fires after all built-in meta boxes have been added.
Receives 2 parameters: $post_type, $post.
There is also the similar add_meta_boxes_{post_type} hook. It fires only on the editing screen for the {post_type} post type, allowing the post type to be specified directly. Because the post type is known in advance, this hook receives only one parameter: $post.
Usage
add_action( 'add_meta_boxes', 'wp_kama_add_meta_boxes_action', 10, 2 );
/**
* Function for `add_meta_boxes` action-hook.
*
* @param string $object_type The type of the current object that meta boxes were added to.
* Can be 'post', 'page', custom post types, 'comment', or
* 'link'.
* @param WP_Post|WP_Comment|object $object The post, comment, or link object. Type varies depending on
* `$object_type`.
*
* @return void
*/
function wp_kama_add_meta_boxes_action( $object_type, $object ){
// action...
}
Parameters
- $post_type(string)
- The name of the post type whose editing screen invoked the hook.
- $post(WP_Post)
- The post object: a WP_Post object.
Examples
#1 Add a meta box to the post editing screen
add_action( 'add_meta_boxes', 'adding_custom_meta_boxes', 10, 2 );
function adding_custom_meta_boxes( $post_type, $post ) {
add_meta_box( 'my-meta-box', 'My meta box', 'render_my_meta_box', 'post', 'normal', 'default' );
}
function render_my_meta_box(){
echo 'Meta box HTML';
}
#2 Use the similar add_meta_boxes_{post_type} hook
This code does the same thing as the first example, but the post type is specified directly in the hook name.
add_action( 'add_meta_boxes_'.'post', 'adding_custom_meta_boxes' );
function adding_custom_meta_boxes( $post ) {
add_meta_box( 'my-meta-box', 'My meta box', 'render_my_meta_box', 'post', 'normal', 'default' );
}
function render_my_meta_box(){
echo 'Meta box HTML';
}Changelog
| Since 3.0.0 | Introduced. |
Where the hook is called
add_meta_boxes
wp-admin/includes/meta-boxes.php 1692
do_action( 'add_meta_boxes', $post_type, $post );
wp-admin/edit-link-form.php 37
do_action( 'add_meta_boxes', 'link', $link );
wp-admin/edit-form-comment.php 418
do_action( 'add_meta_boxes', 'comment', $comment );