remove_meta_box()

最后更新于:2021-11-27 21:42:48

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

Removes a meta box from one or more screens.

参数

$id

(string) (Required) Meta box ID (used in the ‘id’ attribute for the meta box).

$screen

(string|array|GC_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, GC_Screen object, or array of screen IDs.

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

源文件

文件: gc-admin/includes/template.php

function remove_meta_box( $id, $screen, $context ) {
	global $gc_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( $gc_meta_boxes ) ) {
		$gc_meta_boxes = array();
	}
	if ( ! isset( $gc_meta_boxes[ $page ] ) ) {
		$gc_meta_boxes[ $page ] = array();
	}
	if ( ! isset( $gc_meta_boxes[ $page ][ $context ] ) ) {
		$gc_meta_boxes[ $page ][ $context ] = array();
	}

	foreach ( array( 'high', 'core', 'default', 'low' ) as $priority ) {
		$gc_meta_boxes[ $page ][ $context ][ $priority ][ $id ] = false;
	}
}
add_action( 'admin_menu', 'gcdocs_remove_custom_taxonomy' );

/**
 * Remove the genre taxonomy box from the movie edit screen
 */
function gcdocs_remove_custom_taxonomy()
{
	$custom_taxonomy_slug = 'genre';
	$custom_post_type = 'movies';
	
	remove_meta_box('tagsdiv-'.$custom_taxonomy_slug, $custom_post_type, 'side' );
}

add_action('gc_dashboard_setup', 'gcdocs_remove_dashboard_widgets');

/**
 * Remove all dashboard widgets
 */
function gcdocs_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');   // GeChiUI blog
	remove_meta_box('dashboard_secondary', 'dashboard', 'side');   // Other GeChiUI News
	// use 'dashboard-network' as the second parameter to remove widgets from a network dashboard.
}

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' );
if ( is_admin() ) {
	add_action( 'admin_menu', 'gcdocs_remove_meta_boxes' );
}

/**
 * Remove meta boxes from the post edit screens
 */
function gcdocs_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' );
	}
}
<?php 
add_action( 'admin_menu' , 'gcdocs_remove_page_fields' );

/**
 * Remove meta boxes from page screen
 */
function gcdocs_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 
}
?>

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