最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - How to target post and pages and not all post types in admin?

programmeradmin0浏览0评论

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

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');
}
Share Improve this question asked Feb 23, 2020 at 23:14 BlueDogRanchBlueDogRanch 1705 silver badges25 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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

发布评论

评论列表(0)

  1. 暂无评论