remove_meta_box()
云策文档标注
概述
remove_meta_box() 函数用于从 WordPress 管理后台的一个或多个屏幕中移除元框(meta box)。开发者需注意调用时机,以确保在元框添加后才能成功移除。
关键要点
- 函数接受三个参数:$id(元框ID)、$screen(屏幕ID、WP_Screen对象或数组)和$context(上下文,如'normal'、'side'、'advanced')。
- 移除元框必须在元框添加之后进行,推荐使用 add_meta_boxes 钩子以确保正确顺序。
- 从 WordPress 4.4.0 开始,$screen 参数支持屏幕ID数组,允许批量移除。
- 对于 Gutenberg 编辑器,传统移除方法可能无效,需使用 JavaScript 或 remove_post_type_support() 等替代方案。
代码示例
// 移除仪表盘所有小工具
add_action('wp_dashboard_setup', 'wpdocs_remove_dashboard_widgets');
function wpdocs_remove_dashboard_widgets(){
remove_meta_box('dashboard_right_now', 'dashboard', 'normal');
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
// 更多移除代码...
}注意事项
- 使用 admin_menu 钩子可能过早,对于插件生成的元框,建议使用 do_meta_boxes 钩子。
- 移除自定义分类法元框时,可通过 register_taxonomy() 设置 'meta_box_cb' => false 来避免显示。
- 在 Gutenberg 编辑器中,移除面板需使用 wp.data.dispatch('core/edit-post').removeEditorPanel() 方法。
原文内容
Removes a meta box from one or more screens.
Parameters
$idstringrequired-
Meta box ID (used in the
'id'attribute for the meta box). $screenstring|array|WP_Screenrequired-
The screen or screens on which the meta box is shown (such as a post type,
'link', or'comment'). Accepts a single screen ID, WP_Screen object, or array of screen IDs. $contextstringrequired-
The context within the screen where the box is set to display.
Contexts vary from screen to screen. Post edit screen contexts include'normal','side', and'advanced'. Comments screen contexts include'normal'and'side'. Menus meta boxes (accordion sections) all use the'side'context.
Source
function remove_meta_box( $id, $screen, $context ) {
global $wp_meta_boxes;
if ( empty( $screen ) ) {
$screen = get_current_screen();
} elseif ( is_string( $screen ) ) {
$screen = convert_to_screen( $screen );
} elseif ( is_array( $screen ) ) {
foreach ( $screen as $single_screen ) {
remove_meta_box( $id, $single_screen, $context );
}
}
if ( ! isset( $screen->id ) ) {
return;
}
$page = $screen->id;
if ( ! isset( $wp_meta_boxes ) ) {
$wp_meta_boxes = array();
}
if ( ! isset( $wp_meta_boxes[ $page ] ) ) {
$wp_meta_boxes[ $page ] = array();
}
if ( ! isset( $wp_meta_boxes[ $page ][ $context ] ) ) {
$wp_meta_boxes[ $page ][ $context ] = array();
}
foreach ( array( 'high', 'core', 'default', 'low' ) as $priority ) {
$wp_meta_boxes[ $page ][ $context ][ $priority ][ $id ] = false;
}
}
Skip to note 17 content
Codex
To remove all the widgets from the dashboard screen, use:
add_action('wp_dashboard_setup', 'wpdocs_remove_dashboard_widgets'); /** * Remove all dashboard widgets */ function wpdocs_remove_dashboard_widgets(){ remove_meta_box('dashboard_right_now', 'dashboard', 'normal'); // Right Now remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal'); // Recent Comments remove_meta_box('dashboard_incoming_links', 'dashboard', 'normal'); // Incoming Links remove_meta_box('dashboard_plugins', 'dashboard', 'normal'); // Plugins remove_meta_box('dashboard_quick_press', 'dashboard', 'side'); // Quick Press remove_meta_box('dashboard_recent_drafts', 'dashboard', 'side'); // Recent Drafts remove_meta_box('dashboard_primary', 'dashboard', 'side'); // WordPress blog remove_meta_box('dashboard_secondary', 'dashboard', 'side'); // Other WordPress News // use 'dashboard-network' as the second parameter to remove widgets from a network dashboard. }Skip to note 18 content
phpdevca
I was having problems removing the “author” div on a custom post type. The solution was to use a different hook. Instead of using the “admin_menu” hook, the “admin_head” hook worked.
e.g.
Skip to note 19 content
Codex
Here is an example that removes the Custom Fields box from the Post edit screen.
Skip to note 20 content
Codex
To remove meta boxes created by plugins,
admin_menuis fired too early, usedo_meta_boxesinstead. This is helpful for instances when you want to limit meta boxes by user capability:add_action( 'do_meta_boxes', 'wpdocs_remove_plugin_metaboxes' ); /** * Remove Editorial Flow meta box for users that cannot delete pages */ function wpdocs_remove_plugin_metaboxes(){ if ( ! current_user_can( 'delete_others_pages' ) ) { // Only run if the user is an Author or lower. remove_meta_box( 'ef_editorial_meta', 'post', 'side' ); // Remove Edit Flow Editorial Metadata } }Skip to note 21 content
John Dorner
This will only remove the excerpt if you are not using Gutenberg editor.
If you are using the Gutenberg editor, use:
function wpdocs_remove_page_excerpt_field() { remove_post_type_support( 'page', 'excerpt' ); } add_action( 'admin_init', 'wpdocs_remove_page_excerpt_field' );Skip to note 22 content
beinoriusju
Gutenberg editor does not recognize the old way of removing panels. If you like me have custom taxonomies you should remove them with JavaScript.
add_action( 'admin_enqueue_scripts', 'wpdocs_my_admin_scripts' ); function wpdocs_my_admin_scripts( $hook ) { wp_enqueue_script( 'wpdocs-my-editor-script', 'my-editor-script.js' ); $data = array( 'hook' => $hook ); wp_localize_script( 'wpdocs-my-editor-script', 'my_editor_script', $data ); }Then your script…
window.addEventListener( 'load', () => { if ( [ 'post.php', 'post-new.php' ].indexOf( my_editor_script.hook ) > -1 ) { wp.data.dispatch( 'core/edit-post' ).removeEditorPanel( 'taxonomy-panel-MY-TAXONOMY-NAME' ) ; } } )Other panels can be removed also.
Skip to note 23 content
David
It’s easier not to show meta boxes for custom taxonomies in the editor by adding:
'meta_box_cb' => false,This is added within register_taxonomy().
For example:
function custom_tags(){ $singular_name = 'Example'; $plural_name = 'Examples'; $taxonomy_slug = sanitize_title_with_dashes($singular_name); // Taxonomy key, must not exceed 32 characters. $category_taxonomy = false; // False for Tags, True for Categories $post_type = 'custom_post_type'; // Object type or array of object types with which the taxonomy should be associated. $labels = array( 'name' => _x( $plural_name, 'Taxonomy Plural Name' ), 'singular_name' => _x( $singular_name, 'Taxonomy Singular Name' ), 'search_items' => __( 'Search ' . $plural_name ), 'popular_items' => __( 'Popular ' . $plural_name ), 'all_items' => __( 'All ' . $plural_name ), 'parent_item' => null, 'parent_item_colon' => null, 'edit_item' => __( 'Edit ' . $singular_name ), 'update_item' => __( 'Update ' . $singular_name ), 'add_new_item' => __( 'Add New ' . $singular_name ), 'new_item_name' => __( 'New ' . $singular_name . ' Name' ), 'separate_items_with_commas' => __( 'Separate ' . $plural_name . ' With Commas' ), 'add_or_remove_items' => __( 'Add or Remove ' . $plural_name ), 'choose_from_most_used' => __( 'Choose From the Most Used ' . $plural_name ), 'menu_name' => __( $plural_name ), ); register_taxonomy( $taxonomy_slug, $post_type, array( 'hierarchical' => $category_taxonomy, 'labels' => $labels, 'show_ui' => true, 'show_in_rest' => true, 'show_admin_column' => true, 'meta_box_cb' => false, 'update_count_callback' => '_update_post_term_count', 'query_var' => true, 'rewrite' => array( 'slug' => $taxonomy_slug ), ) ); } add_action( 'init', 'custom_tags' );Skip to note 24 content
jemand1
Doesn’t work with the new block editor
Skip to note 25 content
Codex
Here is another example that removes the Excerpt meta box from the Page edit screen,
Skip to note 26 content
Codex
If you want to remove a custom taxonomy box from a custom post type edit screen, you can use this:
add_action( 'admin_menu', 'wpdocs_remove_custom_taxonomy' ); /** * Remove the genre taxonomy box from the movie edit screen */ function wpdocs_remove_custom_taxonomy() { $custom_taxonomy_slug = 'genre'; $custom_post_type = 'movies'; remove_meta_box('tagsdiv-'.$custom_taxonomy_slug, $custom_post_type, 'side' ); }Skip to note 27 content
John Sundberg
I have several client sites using the LearnDash LMS plugin. For some unknown-to-me reason LearnDash includes a LearnDash Course Grid Settings metabox on EVERY post, page, and custom post type Add or Edit screen.
In an effort to make my clients’ lives easier and less confusing I wanted to remove that metabox from all post types, including pages. To make matters a bit more complicated, sometimes that metabox appears in the “normal-sortables” div, and sometimes in the “advanced-sortables” div.
Here’s a simple function that removes that metabox no matter where it appears, without creating an array of all the post types. Modify as needed for your application.
“`php
/**
* Removes the LearnDash Course Grid Settings metabox
* that appears on all post types.
*/
add_action( ‘do_meta_boxes’, function () {
$post_type = get_post_type() ;
remove_meta_box( ‘learndash-course-grid-meta-box’, $post_type, ‘advanced’ );
remove_meta_box( ‘learndash-course-grid-meta-box’, $post_type, ‘normal’ );
} );
“`
Skip to note 28 content
Codex
This example removes certain meta boxes from the post edit screens of both the Post and Link post types for non-administrators.
if ( is_admin() ) { add_action( 'admin_menu', 'wpdocs_remove_meta_boxes' ); } /** * Remove meta boxes from the post edit screens */ function wpdocs_remove_meta_boxes() { if ( ! current_user_can( 'manage_options' ) ) { remove_meta_box( 'linktargetdiv', 'link', 'normal' ); remove_meta_box( 'linkxfndiv', 'link', 'normal' ); remove_meta_box( 'linkadvanceddiv', 'link', 'normal' ); remove_meta_box( 'postexcerpt', 'post', 'normal' ); remove_meta_box( 'trackbacksdiv', 'post', 'normal' ); remove_meta_box( 'postcustom', 'post', 'normal' ); remove_meta_box( 'commentstatusdiv', 'post', 'normal' ); remove_meta_box( 'commentsdiv', 'post', 'normal' ); remove_meta_box( 'revisionsdiv', 'post', 'normal' ); remove_meta_box( 'authordiv', 'post', 'normal' ); remove_meta_box( 'sqpt-meta-tags', 'post', 'normal' ); } }Skip to note 29 content
Codex
This example removes the Comments, Author and Comments Status meta boxes from the Page edit screen,
commentstatusdivit actually disables comments on any new posts created in $screen. If you want to not see it use thehidden_meta_boxeshookSkip to note 30 content
Codex
Even the Publish box can be removed if desired:
add_action( 'admin_menu', 'wpdocs_remove_publish_box' ); /** * Remove the Publish box */ function wpdocs_remove_publish_box() { remove_meta_box( 'submitdiv', 'custom_post_id', 'side' ); }Skip to note 31 content
danhgilmore
In order to remove a widget from the Network Dashboard, you must use wp_network_dashboard_setup hook.
/** * Remove the WordPress News & Events widget from Network Dashboard */ function wpdocs_remove_network_dashboard_widgets() { remove_meta_box( 'dashboard_primary', 'dashboard-network', 'side' ); } add_action( 'wp_network_dashboard_setup', 'wpdocs_remove_network_dashboard_widgets' );Skip to note 32 content
pierrelapalu
If you want to remove all default metaboxes created for your custom taxonomies, but don’t want to set the ‘show_ui’ => false in the registration of the taxonomy (because it would remove it from the menu), here’s a function for that:
function mytheme_remove_all_metaboxes() { $args = array( 'public' => true, '_builtin' => false ); $post_types = get_post_types($args); foreach ($post_types as $i => $post_type) { $taxonomy_objects = get_object_taxonomies( $post_type, 'object' ); foreach ($taxonomy_objects as $j => $tax_obj) { if($tax_obj->hierarchical){ $div_id = $tax_obj->name . 'div'; } else { $div_id = 'tagsdiv-' . $tax_obj->name; } remove_meta_box($div_id, $post_type, 'side'); } } } add_action('admin_menu', 'mytheme_remove_all_metaboxes', 999);