I have a parent theme that creates a number of custom meta boxes that are added to the “post” and “pages” post types as well as a number of post types also created by the theme.
In my child theme, I have created some new custom post types, so I would like to add a function somewhere in my child theme to add these pre-existing meta boxes to the new post types defined in my child theme.
I have tried a number of things already but in every case, short of editing the meta box file in my parent theme, the meta box fails to be added to my custom post type.
I would love to learn how to do this. Can someone here teach me how I would do this?
Thanks
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' );