3

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

2 Answers 2

1

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' );
0

If you can find the function that the parent theme defines and hooks into add_meta_boxes, you can remove the action and redefine it in your child theme.

function my_child_theme_meta_box_override_cb() {
    $post_types = array( 'post', 'page', 'my_other_post_type' );
    // copy the add_meta_box function from the parent theme hook below...
}

function wpse_override_meta_box_action(){
    remove_action( 'add_meta_boxes', 'parent_theme_meta_box_hook_cb' );
    add_action( 'add_meta_boxes', 'my_child_theme_meta_box_override_cb' );
}
add_action( 'after_setup_theme', 'wpse_override_meta_box_action' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.