返回介绍

remove_meta_box()

发布于 2017-09-11 10:08:32 字数 8195 浏览 1029 评论 0 收藏 0

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

Removes a meta box from one or more screens.


description


参数

$id
(string) (Required) Meta box ID (used in the 'id' attribute for the meta box).

所要移除的 Meta 模块的 HTML 的 id 属性。部分可用的 id 如下:

  • 'authordiv' – 作者模块
  • 'categorydiv' – 分类模块
  • 'commentstatusdiv' – 评论状态模块
  • 'commentsdiv' – 评论模块
  • 'formatdiv' – 文章格式模块
  • 'pageparentdiv' – 页面属性模块
  • 'postcustom' – 自定义字段模块
  • 'postexcerpt' – 摘要模块
  • 'postimagediv' – 特色图像模块
  • 'revisionsdiv' – 版本模块
  • 'slugdiv' – 别名模块
  • 'submitdiv' – 发布 模块
  • 'tagsdiv-post_tag' – 标签模块
  • 'trackbacksdiv' – 发送 trackback 模块
$screen
(string|array|WP_Screen) (Required) 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.

要从那个编辑界面移除 Meta 模块,可用值:

  • 'post'  - 文章编辑界面
  • 'page'  - 页面编辑界面
  • 'attachment'  - 附件编辑界面
  • 'link' - 链接编辑界面
  • 'dashboard' - 仪表盘
  • 或者已注册的自定义文章类型的编辑界面,例如 'my-product'
$context
(string) (Required) 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.

所要删除的 Meta 模块所在的位置,可选值: 'normal','advanced' 和 'side'。


源代码

File: wp-admin/includes/template.php

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

更新日志

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

相关函数

Uses

  • wp-admin/includes/screen.php: get_current_screen()
  • wp-admin/includes/template.php: convert_to_screen()
  • wp-admin/includes/template.php: remove_meta_box()

Used By

  • wp-admin/includes/template.php: remove_meta_box()

Here is an example that removes the Custom Fields box from the Post edit screen.


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

Here is another example that removes the Excerpt meta box from the Page edit screen,


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

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

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

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

This example removes the Comments, Author and Comments Status meta boxes from the Page edit screen,


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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文