WordPress函数remove_meta_box()移除Meta模块

描述:

移除Meta模块

用法:

<?php remove_meta_box( $id, $page, $context ); ?>

参数:

$id

(string) (必填) 要删除的HTML元素的id属性的值。下面给出了一些可用的id值:

  • ‘authordiv’ – 作者
  • ‘categorydiv’ – 分类
  • ‘commentstatusdiv’ – 评论状态
  • ‘commentsdiv’ – 评论
  • ‘formatdiv’ – 文章形式
  • ‘pageparentdiv’ – 属性
  • ‘postcustom’ – 自定义字段
  • ‘postexcerpt’ – 摘要
  • ‘postimagediv’ – 特色图像
  • ‘revisionsdiv’ – 修订版本
  • ‘slugdiv’ – bie名
  • ‘submitdiv’ – 日期、状态、最后修订日期
  • ‘tagsdiv-post_tag’ – 标签
  • ‘{$tax-name}div’ – 自定义分类
  • ‘trackbacksdiv’ – Trackbacks

默认值: None

$page

(string) (必填) 要删除哪个文章类型下的Meta模块,例如:

  • ‘post’
  • ‘page’
  • ‘attachment’
  • ‘link’
  • ‘dashboard’
  • 或任意自定义文章类型, 例如. ‘my-product’

默认值: None

$context

(string) (必填) ‘normal’, ‘advanced’, or ‘side’.

默认值: None

示例:

if (is_admin()) :
function my_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');
 }
}
add_action( 'admin_menu', 'my_remove_meta_boxes' );
endif;

源文件:

/**
 * Remove a meta box from an edit form.
 *
 * @since 2.6.0
 *
 * @global array $wp_meta_boxes
 *
 * @param string        $id      String for use in the 'id' attribute of tags.
 * @param string|object $screen  The screen on which to show the box (post, page, link).
 * @param string        $context The context within the page where the boxes should show ('normal', 'advanced').
 */
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 );

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