Out-of-the-box, the meta boxes that WordPress displays on the dashboard aren’t exactly overkill. I mean, if you’re a blogger, then I think the chances are strong that you’ll need:
- And maybe the Excerpt feature (depending on your theme)
But if you’re building a solution for someone else where that information is irrelevant, wouldn’t it be worth removing the WordPress meta boxes to make the dashboard a little bit cleaner with less irrelevant options?
Remove WordPress Meta Boxes
Anyway, the two ways I’ve often seen it done is when:
- Developers look for the check boxes under the Screen Options and then trigger the `click` event.
- Developers sniff out the meta boxes ID and then use jQuery’s `hide()` method to hide the meta boxes from the page.
Sure, these work, but you’re still faced with the challenges of accessibility and with the fact that you’re modifying something that should probably be done via CSS.
Unless a hook exists.
And that’s really the mentality that we, as WordPress developers, should have whenever we’re attempting to introduce, remove, or modify anything as it relates to WordPress both in the dashboard and on the front-end.
Specifically, we need to ask ourselves:
Given what I need to do, is there a hook that makes this available?
And this is this case (and many, many cases), there is. We can take advantage of the
default_hidden_meta_boxes hook. Though I was unable to find a Codex page for this particular hook, you can see it in core’s source code.
Using The Hook
The function accepts two arguments:
- An array of meta boxes that should be hidden
- The current screen being displayed
This allows us to modify the existing array of hidden meta boxes (or create our own) and only trigger it on specific screen’s, as well.
So let’s say that we have a custom post type called
acme_post_type and we want to hide the Categories, Author, Post Excerpt, and Slug meta box.
To do this, we can define the following function:
First, the code checks to see if we’re on the screen for the custom post type. If so, then we define a new array that includes the IDs of the meta boxes that we want to hide. After that, we the array to WordPress and the end-result of the function will be that the specified meta boxes will be hidden from view.
In terms of getting the ID of the meta boxes that you want to hide, you can use the Developer Tools of your preferred browser to inspect the elements on the page. You can also use a reference on this Codex page.
Leave a Reply
You must be logged in to post a comment.