Using WordPress ‘add_meta_boxes_{$post_type}’ PHP action

The add_meta_boxes_{$post_type} WordPress action allows you to add custom meta boxes after all built-in meta boxes have been added for a specific post type.

Usage

add_action('add_meta_boxes_{$post_type}', 'my_custom_meta_boxes', 10, 2);

function my_custom_meta_boxes($post_type, $post) {
    // Your custom code here
}

Parameters

  • $post_type (string) – The dynamic portion of the hook name which refers to the post type of the post. Possible hook names include: add_meta_boxes_post, add_meta_boxes_page, add_meta_boxes_attachment.
  • $post (WP_Post) – The WordPress Post object.

More information

See WordPress Developer Resources: add_meta_boxes_{$post_type}

Examples

Add a custom meta box to the ‘post’ post type

Add a custom meta box with the title ‘My Custom Meta Box’ to the ‘post’ post type.

add_action('add_meta_boxes_post', 'add_my_custom_meta_box');
function add_my_custom_meta_box() {
    add_meta_box('my_custom_meta_box', 'My Custom Meta Box', 'display_my_custom_meta_box', 'post', 'normal', 'high');
}

function display_my_custom_meta_box($post) {
    // Display the content of the custom meta box
}

Add a custom meta box to the ‘page’ post type

Add a custom meta box with the title ‘Page Options’ to the ‘page’ post type.

add_action('add_meta_boxes_page', 'add_page_options_meta_box');
function add_page_options_meta_box() {
    add_meta_box('page_options', 'Page Options', 'display_page_options_meta_box', 'page', 'side', 'default');
}

function display_page_options_meta_box($post) {
    // Display the content of the custom meta box
}

Remove built-in meta boxes from the ‘post’ post type

Remove the ‘Excerpt’ and ‘Custom Fields’ built-in meta boxes from the ‘post’ post type.

add_action('add_meta_boxes_post', 'remove_built_in_meta_boxes', 10);
function remove_built_in_meta_boxes() {
    remove_meta_box('postexcerpt', 'post', 'normal');
    remove_meta_box('postcustom', 'post', 'normal');
}

Add a custom meta box to a custom post type ‘books’

Add a custom meta box with the title ‘Book Information’ to the custom post type ‘books’.

add_action('add_meta_boxes_books', 'add_book_information_meta_box');
function add_book_information_meta_box() {
    add_meta_box('book_information', 'Book Information', 'display_book_information_meta_box', 'books', 'normal', 'high');
}

function display_book_information_meta_box($post) {
    // Display the content of the custom meta box
}

Change the position of a built-in meta box

Move the ‘Featured Image’ meta box from the ‘side’ to the ‘normal’ position for the ‘post’ post type.

add_action('add_meta_boxes_post', 'move_featured_image_meta_box', 20);
function move_featured_image_meta_box() {
    remove_meta_box('postimagediv', 'post', 'side');
    add_meta_box('postimagediv', __('Featured Image'), 'post_thumbnail_meta_box', 'post', 'normal','low');
}

Add a custom meta box to all post types

Add a custom meta box with the title ‘Global Options’ to all post types.

add_action('add_meta_boxes', 'add_global_options_meta_box', 10, 2);
function add_global_options_meta_box($post_type, $post) {
    add_meta_box('global_options', 'Global Options', 'display_global_options_meta_box', $post_type, 'side', 'default');
}

function display_global_options_meta_box($post) {
    // Display the content of the custom meta box
}