Remove a meta_box in the WordPress post/page screen

Yesterday I had a friend write me on Twitter asking about remove_mata_box. He was having issues getting the function to fire properly (or at at for that matter).

I was out last night and just got a chance to open up my computer and test out some code. While the WordPress codex is intact and had example code. It simply wasn’t working.

Codex Example

function remove_featured_image_field() {
	remove_meta_box( 'postimagediv' , 'page' , 'normal' ); 
}
add_action( 'admin_menu' , 'remove_featured_image_field' );
?>

I added the above function, but like mentioned before had no luck. So I wrote up a quick plugin to handle the task and came up with this (which still didn’t work):

$remove = new Frosty_Remove_Meta_Box();

class Frosty_Remove_Meta_Box {
	
	function Frosty_Remove_Meta_Box() {
		$this->__construct();
	}
	
	/* To infinity and beyond */
	function __construct() {
		add_action( 'admin_init, array( __CLASS__, 'remove_meta_box' ) );
		add_action( 'admin_menu, array( __CLASS__, 'remove_meta_box' ) );
	}
	
	function remove_meta_box() {
		remove_meta_box( 'postimagediv', 'post', 'side' );
	}
	
}

After trying both admin_init & admin_menu I scowered Google and came across a fix my buddy Michael suggested on a WordPress forum. Use admin_head. And so I did, and it worked!

The final code

$remove = new Frosty_Remove_Meta_Box();

class Frosty_Remove_Meta_Box {
	
	function Frosty_Remove_Meta_Box() {
		$this->__construct();
	}
	
	/* To infinity and beyond */
	function __construct() {
		add_action( 'admin_head', array( __CLASS__, 'remove_meta_box' ) );
	}
	
	function remove_meta_box() {
                // Remove the meta box for all post types. (post,page,custom_post)
		$post_types = get_post_types( array( 'public' => true ), 'objects' );
		
		foreach ( $post_types as $type ) {
			remove_meta_box( 'postimagediv', $type->name, 'side' );
		}
	}
	
}

Thoughts? Care to add on to this?

Austin
Austin

πŸ’πŸ½β€β™‚οΈ Husband to Jeana.
⚾️ Dodgers & Brewers.
πŸ’» PHP Engineer.
πŸŒπŸΌβ€β™‚οΈGolfer; ~14 HDC.
🌱 Hydroponic Gardner.
🍿 Plex nerd.
πŸš™ '15 WRX, '22 Model 3 LR, & '66 Corvette C2.

Follow me on Twitter @TheFrosty & Instagram @TheFrosty.

Articles: 292

One comment

  1. Finally! I was also struggling to remove the featured image meta box but by changing my admin_init function to admin_head it works! Thanks for the tip!

Comments are closed.