I've got a strange issue with a plugin I'm modifying. What happens is that this plugin adds meta boxes in an dashboard options page so scripts and style sheets can be globally added to the header and footer areas of a site via wp_footer() andwp_header()`.

This plugin also adds one meta box to each page and post editor, and I think that's where the problem is; it also adds meta boxes to other plugin admin panels, which of course I don't want. I.e., a meta "footer" box is added to the field collections in Advanced Custom Fields on the backend.

I think the culprit is the the foreach ( get_post_types( '', 'names' ) as $type ). How would I change this to only add the meta boxes to post and page editor and not to all post types throughout the whole admin area?

function admin_init() {

    // register settings for sitewide script
    register_setting( 'mr-header-and-footer-scripts', 'shfs_insert_header', 'trim' );
    register_setting( 'mr-header-and-footer-scripts', 'shfs_insert_footer', 'trim' );

    // add meta box to all post types
    foreach ( get_post_types( '', 'names' ) as $type ) {
        add_meta_box('shfs_all_post_meta', esc_html__('Add script to footer', 'mr-header-and-footer-scripts'), 'shfs_meta_setup', $type, 'normal', 'high');
    }

    add_action('save_post','shfs_post_meta_save');
}
有帮助吗?

解决方案

You just need to change the get_post_types( '', 'names' ) to array( 'post', 'page' ). I.e. Manually specify the post types.

许可以下: CC-BY-SA归因
scroll top