How to remove WordPress Meta Box

How to remove WordPress Meta Box

You may be using a plugin in your site and it comes with an unwanted meta box. This quick snippet will demonstrate how to remove a WordPress Meta Box from your edit screen.

What is a WordPress Meta Box?

A meta box is almost self explanatory. In the WordPress dashboard you may notice some draggable boxes especially on your post edit screen. Those boxes contains meta details for that post or registered post type. The widgets on the dashboard are also considered meta boxes as well. In another tutorial, we will look into how to add a meta box from scratch but for this short tut, we’re going to get rid of nuisance boxes.

The WP Codex list a function called remove_meta_box and we are going to use it to get the job done. The function uses 3 paramaters; id, page, context.

ID refers to the meta box. This can be found within the plugin code or can be found by inspecting the element.

Page refers to the screen which can be the dashboard, any registered post type, attachments etc.

Context is the position of the meta box and the options are normal, advanced and side.

Digging In

With that said, let’s write our function. For the purpose of this tutorial, we are going to remove a meta box from a post type, so we’ll call our function through the add_meta_boxes action

PHP

1

2

3

4

add_action('add_meta_boxes','mpl_remove_metabox',99);

functionmpl_remove_metabox(){

remove_meta_box('metabox_id','post','normal');

}

Extending Our Code

What we’ve written so far will remove the meta box with the ID “metabox_id” from the post post type. Next, we’ll make the function more dynamic to remove from multiple post types.

PHP

1

2

3

4

5

6

7

add_action('add_meta_boxes','mpl_remove_metabox',99,1);

functionmpl_remove_metabox($post_type){

$post_types_to_remove=array('post','page','slideshow');

if(in_array($post_type,$post_types_to_remove)){

remove_meta_box('metabox_id',$post_type,'normal');

}

}

Real Life Examples

Here is how to remove WordPress SEO by Yoast

PHP

1

2

3

4

5

6

7

add_action('add_meta_boxes','mpl_remove_metabox',99,1);

functionmpl_remove_metabox($post_type){

$post_types_to_remove=array('slideshow');

if(in_array($post_type,$post_types_to_remove)){

remove_meta_box('wpseo_meta',$post_type,'normal');

}

}

Removing Custom Post Fields Meta Box

PHP

1

2

3

4

5

6

7

add_action('add_meta_boxes','mpl_remove_metabox',99,1);

functionmpl_remove_metabox($post_type){

$post_types_to_remove=array('post');

if(in_array($post_type,$post_types_to_remove)){

remove_meta_box('postcustom',$post_type,'normal');

}

}

Notes by the Codex

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.

Resources

If you would like to learn more about removing meta boxes, you can check out the WordPress codex.