Code snippet: Hide post meta boxes in WordPress

Meta boxes are the draggable/collapsible boxes on edit pages in WordPress, and in addition to default meta boxes for things like setting the post author, categories, etc., lots of plugins add their own meta boxes.

It’s easy to get overwhelmed by all these meta boxes. While they can be useful, they’re typically only useful when you actually need them. When you don’t, they just take up space and make it harder to find the features you do need.

Fortunately, WordPress gives you a few ways to hide these meta boxes. I cover three options below.

Meet Screen Options

It’s one of those features that’s always available in WordPress, and yet many people miss it: the Screen Options tab.

The WordPress Screen Options tab
The Screen Options tab in WordPress makes it easy to show or hide meta boxes

Accessible at the top of the screen, this is the easiest way to hide meta boxes. Find one you don’t want to see? Uncheck the box and it will instantly disappear. Neat, right?

Remove the meta boxes completely

Using WordPress’ remove_meta_box function, you can completely eliminate meta boxes from your site (unless you remove the function call, of course).

This is best for meta boxes you know you’ll never need. Once you’ve used remove_meta_box, the meta box in question won’t show up in the Screen Options tab, so you can’t re-add it.

Here’s the code snippet to permanently remove the custom fields metabox, for instance:

In the remove_meta_box function, the first parameter is the ID of the meta box you wish to remove. You can find a full list at the WordPress Codex page for remove_meta_box. The second parameter is the post type you wish to target, so you’re only removing the meta box from a pre-specified type. The final parameter is the position of the meta box. Usually, the position should be “normal” or “side”, depending on which column it’s in. It’s important to note that all three parameters are required.

Change the defaults with the default_hidden_meta_boxes hook

The final method I want to cover, and my favourite, is changing the default hidden meta boxes. This method is ideal because it lets you determine which meta boxes are hidden for users until they decide otherwise in the Screen Options tab. That way, you can set safe defaults while still allowing your users to override those defaults.

To change the defaults, you simply need to hook into the default_hidden_meta_boxes filter and supply your own PHP array listing the meta boxes you’d like hidden by default. In the example below, I hide the author meta box and the revisions meta box. This way, they’re hidden for users unless they’ve decided to enable them in Screen Options.

You’ll notice that in the add_action function call, we pass two numerical parameters at the end: 10 and 2. In our example, 10 is setting the priority of the hook, and 2 is the number of parameters we want. This lets us access the $screen variable, which lets us determine which post type we want our defaults to apply to.

I hope these strategies can help you customise your WordPress admin to make things easier for you and your clients. If you have any questions, leave them in the comments below!

No Comments »

No comments yet.
Start the conversation by leaving a comment!

Leave a comment