remove_meta_box() WordPress Function

The remove_meta_box() function is used to remove a meta box from a post or page screen in the WordPress admin. This function can be used in conjunction with the add_meta_box() function to add meta boxes as needed.

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

Removes a meta box from one or more screens.


Parameters

$id

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

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

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


Top ↑

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 wp-admin/edit-form-advanced.php 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.


Top ↑

Source

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


Top ↑

Changelog

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

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.