当前浏览:首页 / WordPress函数 / remove_meta_box()

remove_meta_box( string $id, string|array|WP_Screen $screen, string $context )

从一个或多个屏幕界面中删除meta组框

meta_box meta组框

removemore...


参数

$id

(string)(必填) meta box ID(用于元组框的'id'属性)。

$screen

(string|array|WP_Screen)(必填) 显示meta box的一个或多个屏幕(如文章类型,'link',或'comment')。接受单个屏幕ID、WP_Screen对象或屏幕ID数组。

$context

(string)(必填) 屏幕中设置显示框位置的上下文,上下文因屏幕而异。文章编辑屏幕上下文包括'normal'、'side'和'advanced'。评论屏幕上下文包括'normal'和'side'。菜单元框(手风琴部分)都使用'side'上下文。


更多信息

由于在添加meta组框之前无法删除它,因此确保对remove_meta_box()的调用按正确的顺序进行非常重要。仅仅在functions.php中添加对remove_meta_box()的调用可能不会起作用。

add_meta_boxes动作钩子可能是一个很好的候选者,因为大多数元组框都是在文章编辑表单页面上生成的。这个钩子是在wp-admin/edit-form-advanced.php文件中,在所有的meta数据框被成功添加到页面后调用的。这影响到所有出现在管理后台的文章编辑页面(包括自定义帖子类型编辑页面)的元组框(除了那些由主题或插件自定义生成的元组框)。



源码

查看源码 官方文档


更新日志

版本描述
4.4.0$screen参数现在接受屏幕ID数组。
2.6.0开始引入

使用示例

  • 示例1

    下面是一个从文章编辑屏幕中删除自定义字段框的示例。

    <?php 
    add_action( 'admin_menu' , 'wpdocs_remove_post_custom_fields' );
    
    /**
     * Remove Custom Fields meta box
     */
    function wpdocs_remove_post_custom_fields() {
    	remove_meta_box( 'postcustom' , 'post' , 'normal' ); 
    }
    ?>
    
    
  • 示例2

    要从仪表板屏幕中删除所有小工具,请使用:

    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.
    }
    
    
  • 示例3

    若要删除插件创建的meta box,admin_menu触发太早,请改用do_meta_boxes。当您希望通过用户能力限制元框时,这对实例很有帮助:

    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
        }
    }
    
    
  • 示例4

    我在删除自定义post类型上的“author” div时遇到问题。解决方案是使用不同的钩子。“admin_head”钩子起作用,而不是使用“admin_menu”挂钩。

    例如

    <?php 
    add_action( 'admin_head' , 'wpdocs_remove_post_custom_fields' );
     
    /**
     * Remove Custom Fields meta box
     */
    function wpdocs_remove_post_custom_fields() {
        remove_meta_box( 'authordiv' , 'my-cpt' , 'normal' ); 
    }
    ?>
  • 示例5

    这将仅在您未使用Gutenberg编辑器的情况下删除摘录。
    如果您使用的是Gutenberg编辑器,请使用:

    function wpdocs_remove_page_excerpt_field() {
    	remove_post_type_support( 'page', 'excerpt' );
    }
    add_action( 'admin_init', 'wpdocs_remove_page_excerpt_field' );
  • 示例6

    这里是从页面编辑屏幕移除摘录meta box的另一个示例,

    <?php 
    add_action( 'admin_menu' , 'wpdocs_remove_page_excerpt_field' );
    
    /**
     * Remove the Excerpt meta box
     */
    function wpdocs_remove_page_excerpt_field() {
    	remove_meta_box( 'postexcerpt' , 'page' , 'normal' ); 
    }
    ?>
    
    
  • 示例7

    如果要从自定义文章类型编辑屏幕中删除自定义分类框,可以使用以下选项:

    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' );
    }
    
    
  • 示例8

    通过添加以下内容,在编辑器中不显示自定义分类的元框更容易:

    'meta_box_cb' => false,

    这被添加到register_taxonomy()中。

    例如:

    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' );
  • 示例9

    此示例从非管理员的post和Link post类型的post编辑屏幕中删除某些元框。

    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' );
    	}
    }
    
  • 示例10

    此示例从页面编辑屏幕中删除评论、作者和评论状态元框,

    <?php 
    add_action( 'admin_menu' , 'wpdocs_remove_page_fields' );
    
    /**
     * Remove meta boxes from page screen
     */
    function wpdocs_remove_page_fields() {
    	remove_meta_box( 'commentstatusdiv' , 'page' , 'normal' ); //removes comments status
    	remove_meta_box( 'commentsdiv' , 'page' , 'normal' ); //removes comments
    	remove_meta_box( 'authordiv' , 'page' , 'normal' ); //removes author 
    }
    ?>
    
    
  • 示例11

    如果需要,甚至可以删除发布框:

    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' );
    }
    
    
  • 示例12

    要从网络仪表板中删除小工具,必须使用wp_network_dashboard_setup钩子。

    /**
     * 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' );
  • 示例13

    如果要删除为自定义分类创建的所有默认元框,但不想在分类法注册中设置‘show_ui’ => false(因为它将从菜单中删除),这里有一个函数:

    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);
    
  • 示例14

    古腾堡编辑器不认识旧的删除面板的方法。如果你像我一样有自定义分类法,你应该用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 );
    }
    

    然后你的脚本…

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

    也可以移除其他面板。