函数文档

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.

More Information

Because you can’t remove a meta box until it’s been added, it’s important to make sure your call to remove_meta_box() happens in the right sequence. Just adding a call to remove_meta_box() bare in your functions.php will probably not do the trick.

The add_meta_boxes action hook is probably a good candidate since most of your meta boxes are generated on the edit post form page. This hook is called in the <a class="external text" href="https://core.trac.wordpress.org/browser/tags/5.3/src/wp-admin/edit-form-advanced.php#L0" rel="nofollow">wp-admin/edit-form-advanced.php</a> file after all the meta boxes have been successfully added to the page. This affects all meta boxes (conceivably, other than those that are custom generated by a theme or plugin) that appear on post edit pages (including custom post types edit pages) of the administration back-end.

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

Changelog

Version Description
4.4.0 The $screen parameter now accepts an array of screen IDs.
2.6.0 Introduced.

User Contributed Notes

  1. Skip to note 17 content

    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.
    }

  2. Skip to note 20 content

    To remove meta boxes created by plugins, admin_menu is fired too early, use do_meta_boxes instead. 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
        }
    }

  3. Skip to note 22 content

    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.

  4. Skip to note 23 content

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

  5. Skip to note 26 content

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

  6. Skip to note 27 content

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

    } );
    “`

  7. Skip to note 28 content

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

  8. Skip to note 31 content

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

  9. Skip to note 32 content

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