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


1 Answer 1


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

  • 1
    Thanks! That works. Feb 26, 2020 at 21:49

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.