Add pre-existing meta box to new custom post type

This depends on how your parent theme hooks in the meta_boxes. If the callbacks are hooked to add_meta_boxes and written similar to the following from the Codex:

function myplugin_add_meta_box() {

    $screens = array( 'post', 'page' );

    foreach ( $screens as $screen ) {

        add_meta_box(
            'myplugin_sectionid',
            __( 'My Post Section Title', 'myplugin_textdomain' ),
            'myplugin_meta_box_callback',
            $screen
        );
    }
}
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );

Then you aren’t going to be able to add the boxes without hacking the file. That $screens = array( 'post', 'page' ); and the array that follows will prevent it.

Similarly, you won’t be able to add:

function adding_custom_meta_boxes( $post_type, $post ) {
  if ('abcd' == $post_type) return;
  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 );

if ('abcd' == $post_type) return; will prevent it.

However, if it is hooked in with post type specific hooks as recommended

add_action( 'add_meta_boxes_post', 'adding_custom_meta_boxes' );

… then adding others should be easy:

add_action( 'add_meta_boxes_mytype', 'adding_custom_meta_boxes' );
add_action( 'add_meta_boxes_myothertype', 'adding_custom_meta_boxes' );

Leave a Comment