admin管理员组文章数量:1391943
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() and
wp_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() and
wp_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
1 Answer
Reset to default 2You just need to change the get_post_types( '', 'names' )
to array( 'post', 'page' )
. I.e. Manually specify the post types.
本文标签: functionsHow to target post and pages and not all post types in admin
版权声明:本文标题:functions - How to target post and pages and not all post types in admin? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744721160a2621705.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论