WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Plugin API/Action Reference/add meta boxes

See the Advanced Actions section of the Plugin API/Action Reference article.

Description

The hook allows meta box registration for any post type.

Passes two parameters: $post_type and $post.

Note: You can also use add_meta_boxes_{post_type} for best practice, so your hook will only run when editing a specific post type. This will only receive 1 parameter - $post.

Example

function adding_custom_meta_boxes( $post_type, $post ) {
    add_meta_box( 
        'my-meta-box',
        __( 'My Meta Box' ),
        'render_my_meta_box',
        'post',
        'normal',
        'default'
    );
}
add_action( 'add_meta_boxes', 'adding_custom_meta_boxes', 10, 2 );

Now for a post-type specific call.

function adding_custom_meta_boxes( $post ) {
    add_meta_box( 
        'my-meta-box',
        __( 'My Meta Box' ),
        'render_my_meta_box',
        'post',
        'normal',
        'default'
    );
}
add_action( 'add_meta_boxes_post', 'adding_custom_meta_boxes' );

Both will accomplish the same thing. Best practice is to use add_meta_boxes_{post-type} to create less unnecessary hooks for other post types.

Related