4

Is there a way to create a custom post type in WordPress that will only include the default meta boxes and not any other meta box that was added by other plugins?

The idea is to use the custom post to develop a plugin, this plugin will need to have all the functions custom post type but with all the meta boxes that the plugin adds like All in One SEO.

I want to create a clean interface for the user and those extra meta boxes are in the way :-)

1
  • 1
    Meta boxes are added to the global $wp_meta_boxes array. You can inspect the array, empty it entirely, and pass in your desired meta boxes for registration on the desired page(s) you specify in your condition. Look closely at add_meta_box and remove_meta_box in wp-admin/includes/template.php for more help.
    – Adam
    Commented Oct 15, 2013 at 8:52

5 Answers 5

4

Off the top of my head, without actually testing it, this should work. You want to test for is_admin so we don't go running the code on the front end, then also test for the post type being equal to its slug. Edited this because I made a silly mistake before.

Reference this page of the Codex: http://codex.wordpress.org/Function_Reference/remove_meta_box

if( is_admin() ) {
    remove_meta_box('linktargetdiv', '$posttype', 'normal');
    remove_meta_box('linkxfndiv', '$posttype', 'normal');
    remove_meta_box('linkadvanceddiv', '$posttype', 'normal');
    remove_meta_box('postexcerpt', '$posttype', 'normal');
    remove_meta_box('trackbacksdiv', '$posttype', 'normal');
    remove_meta_box('postcustom', '$posttype', 'normal');
    remove_meta_box('commentstatusdiv', '$posttype', 'normal');
    remove_meta_box('commentsdiv', '$posttype', 'normal');
    remove_meta_box('revisionsdiv', '$posttype', 'normal');
    remove_meta_box('authordiv', '$posttype', 'normal');
    remove_meta_box('sqpt-meta-tags', '$posttype', 'normal');
}
4
  • First of all thanks, but ... This will only work on regular metaboxes and not on ones that was created by other plugins like all-in-seo and similar. Commented Oct 15, 2013 at 6:46
  • Right, sorry I got that question backwards in my head. Whoops. You should be able to conditionally remove the function associated with the meta box but it'll often require an additional priority of 99 and knowing exactly what all the meta boxes are, how they are named, and where they are hooked. It's barely practical on a single site and would be a nightmare on multi. Commented Oct 15, 2013 at 7:20
  • Yes that's what I have figure it out before but i wanted to be absolutely sure because the other solution is to develop the entire interface from scratch. Commented Oct 15, 2013 at 14:53
  • If we're talking about non-administrative users, check out something like the CRED plugin from WP-Types. It's literally designed for rapid prototyping a front-end page editor: wp-types.com/home/cred Commented Oct 16, 2013 at 8:01
3

You can find the best answer here: http://codex.wordpress.org/Function_Reference/remove_meta_box

Certain meta boxes can be removed by adding something like the code below to the functions.php file in your WordPress theme:

// hide certain meta boxes on the 'YOUR_CUSTOM_POST_TYPE' custom post type
add_filter('add_meta_boxes', 'hide_meta_boxes_YOUR_CUSTOM_POST_TYPE');
function hide_meta_boxes_YOUR_CUSTOM_POST_TYPE() {
    remove_meta_box('postexcerpt', 'YOUR_CUSTOM_POST_TYPE', 'normal');
    remove_meta_box('trackbacksdiv', 'YOUR_CUSTOM_POST_TYPE', 'normal');
    remove_meta_box('postcustom', 'YOUR_CUSTOM_POST_TYPE', 'normal');
    remove_meta_box('slugdiv', 'YOUR_CUSTOM_POST_TYPE', 'normal');
    remove_meta_box('commentstatusdiv', 'YOUR_CUSTOM_POST_TYPE', 'normal');
    remove_meta_box('commentsdiv', 'YOUR_CUSTOM_POST_TYPE', 'normal');
    remove_meta_box('revisionsdiv', 'YOUR_CUSTOM_POST_TYPE', 'normal');
}
0

Along with the other answers I found that if you create your Custom post type with

$args = array(
    ...
    public => false
    ...
) 

Some metaboxes won't be included such as Wordpress Seo by Yoast for example. On my case im Using the cpt for a popup, so other metaboxes are not needed and the cpt is not needed to be public.

0

some times adding the style display none is a bad option for this purpose work very well and does not make errors on the program. I am using this technique to remove meta boxes from custom post types and normal posts and pages.

to remove the meta box take the main id of the meta box for this you can consult the inspect tool with the right-click option

to make this work just take the meta box id and add a style display: none;

#metab-box-id {
  display:none;
      }
1
  • 1
    I'm confused: you say it's a bad option, but then that's your answer?
    – Rup
    Commented Sep 23, 2022 at 11:43
-1

First of all you shouldn't do it. If a user have installed other plugins, it is because he/she want ot use it, even if your plugin provide all functionalities of that plugins, who say that for the users your plugin is better than others?

And if that users already have tons of settings with other plugins and see them disappear installing your plugin? I bet that users immediately unistall your plugin (and if there is possibility very low votes will come in that case).

Also consider that another plugin can use the same technique to remove your metaboxes. An the another plugin will remove the metaboxes of that plugin, and so on...

What it will be, the war of metaboxes?

My suggestion is to add your metaboxes and let users decide what metaboxes to leave and what to remove (unistalling plugin or unchecking the metabox from the "screen help" tab).

Finally, note that you can't remove metaboxes of a plugin if you don't know the id of those metaboxes. So to do what you want you should install all your "competitors" plugins, look at the id of their metaboxes and remove them using remove_meta_box.

And if a plugin get updated and change the id of its metaboxes?

Believe me, it's and hard, dirty, no-benefit work.

3
  • 2
    "The War of MetaBoxes" it's should be a name of the movie. It's not about functionality some metaboxes are just not relevant to the content that is going to be in the custom post type. For example if post type is managing redirects like simple urls plugin a metabox of a plugin that deals with membership website is just not relevant to that custom post type. Thank you for answer Commented Oct 15, 2013 at 14:48
  • Why would you like to have a CPT with a ton of shit meta boxes that make no sense to your type? Yes, I agree, some might make sense, however a mechanism that would allow you to filter that would have beed awesome - so you can specifically say which are compatible with your CPT. Commented Jun 13, 2016 at 16:38
  • @DragosRizescu that make sense, and can be done, by the end user i.e. the person who runs the website and is aware of all the CPTs and all the plugins installed and can decide which metabox fits and which not. However, as a plugin developer, you should not interfere on this decision. If your plugin register a post type, it can register some metaboxes for just that post type. It's easy and it's on you. If another poorly coded plugin add metaboxes to the CPT your plugin registered, it's not your responsibility (as plugin developer) remove them, but it's up to website owner, who can do that.
    – gmazzap
    Commented Jun 13, 2016 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.