Using WordPress ‘add_meta_boxes’ PHP action

The add_meta_boxes WordPress PHP action is used to add custom meta boxes after all built-in meta boxes have been added.

Usage

add_action( 'add_meta_boxes', 'your_custom_function', 10, 2 );

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

Parameters

  • $post_type (string) – The post type of the current post.
  • $post (WP_Post) – The post object of the current post.

More information

See WordPress Developer Resources: add_meta_boxes

Examples

Adding a custom meta box to posts

This example adds a custom meta box to the post editing screen.

add_action( 'add_meta_boxes', 'my_custom_meta_box', 10, 2 );

function my_custom_meta_box( $post_type, $post ) {
    if ( 'post' === $post_type ) {
        add_meta_box( 'my-meta-box', 'My Custom Meta Box', 'my_meta_box_callback', 'post' );
    }
}

function my_meta_box_callback( $post ) {
    // Display the custom meta box content here
}

Adding a custom meta box to pages

This example adds a custom meta box to the page editing screen.

add_action( 'add_meta_boxes', 'my_page_meta_box', 10, 2 );

function my_page_meta_box( $post_type, $post ) {
    if ( 'page' === $post_type ) {
        add_meta_box( 'my-page-meta-box', 'My Page Meta Box', 'my_page_meta_box_callback', 'page' );
    }
}

function my_page_meta_box_callback( $post ) {
    // Display the custom meta box content here
}

Adding a custom meta box to a custom post type

This example adds a custom meta box to a custom post type called ‘book’.

add_action( 'add_meta_boxes', 'my_book_meta_box', 10, 2 );

function my_book_meta_box( $post_type, $post ) {
    if ( 'book' === $post_type ) {
        add_meta_box( 'my-book-meta-box', 'My Book Meta Box', 'my_book_meta_box_callback', 'book' );
    }
}

function my_book_meta_box_callback( $post ) {
    // Display the custom meta box content here
}

Adding multiple custom meta boxes

This example adds multiple custom meta boxes to different post types.

add_action( 'add_meta_boxes', 'my_multiple_meta_boxes', 10, 2 );

function my_multiple_meta_boxes( $post_type, $post ) {
    if ( 'post' === $post_type ) {
        add_meta_box( 'my-post-meta-box', 'My Post Meta Box', 'my_post_meta_box_callback', 'post' );
    } elseif ( 'page' === $post_type ) {
        add_meta_box( 'my-page-meta-box', 'My Page Meta Box', 'my_page_meta_box_callback', 'page' );
    }
}

function my_post_meta_box_callback( $post ) {
    // Display the custom meta box content for posts here
}

function my_page_meta_box_callback( $post ) {
    // Display the custom meta box content for pages here
}

Removing built-in meta boxes

This example removes some built-in meta boxes from the post editing screen.

add_action( 'add_meta_boxes', 'my_remove_meta_boxes', 10, 2 );

function my_remove_meta_boxes( $post_type, $post ) {if ( 'post' === $post_type ) {
remove_meta_box( 'postcustom', 'post', 'normal' ); // Remove the custom fields meta box
remove_meta_box( 'commentstatusdiv', 'post', 'normal' ); // Remove the comments status meta box
remove_meta_box( 'commentsdiv', 'post', 'normal' ); // Remove the comments meta box
}
}

With these examples, you can easily add or remove meta boxes to customize the editing screen for different post types in your WordPress admin area.