0

I am working on a plugin for which I create a custom post type and several meta boxes on the post page.

However, the theme that I have seems to be adding an ad meta box (Upgrade to our premium theme etc) on the pages of all custom post types.

I consider this an abuse and I don't want that meta box or any other external meta boxes on my post type page, just the ones that I add.

How can I clear all meta boxes except my own?

Edit: I am looking for a general approach to this. If I deploy my plugin to the public, I want to be sure that on my post's page there are no other meta boxes from other themes and plugins, if possible.

  • I assume your issue is solved since there was no follow up. Please accept one of the answers so this issue can be closed. – Jory Hogeveen Apr 21 '17 at 10:35
1

You can use remove_meta_box() to accomplish that.

To use remove_meta_box() you will need 3 pieces of information:

  1. the id of the meta box you want to remove
  2. the screen (or screens) you want it removed from
  3. the context it was added in (e.g., 'side', 'normal', etc)

To get these 3 pieces of info, search thru the source for the theme for add_meta_box() call(s) that are used to add the meta box.

And in case it's not obvious, make sure you call remove_meta_box() in an action that is fired after whatever action the meta box was added in.

For example, if the theme looks like:

add_action ('after_theme_setup', 'add_theme_meta_boxes') ;

function
add_theme_meta_boxes ()
{
    add_meta_box ('ad-to-upgrade-to-pro', 'Upgrade to Pro', 'render_upgrade_ad', 'post', 'normal', 'high') ;

    return ;
}

The your plugin should contain the following:

// make sure you hook to an action that is fired *after* the meta box was added
add_action ('init', 'remove_theme_ad_meta_box') ;

function
remove_theme_ad_meta_box ()
{
    remove_meta_box ('ad-to-upgrade-to-pro', 'post', 'normal') ;

    return ;
}

Edit

To answer the question asked in comment: it is not possible to absolutely guarantee that no other theme/plugin adds meta boxes.

Besides, with an open system like WP, it is not a good idea to do what you are trying to do (IMHO). There are a whole host of "general purpose" plugins out in the wild that have legitimate reasons for adding meta boxes to your CPT's post{,-new}.php screens.

For example, the WordPress SEO plugin adds a meta box to any post type that is "viewable" that allows adding SEO-related metadata to the public page for posts (note: mention of WordPress SEO is not meant as an endorsement, it's just the 1st one that came to mind).

  • I am aware of the remove_meta_box function and I have also been able to find the exact id of the meta box in question in the theme source code. However, I was looking for a general approach, not a specific one. If I deploy my plugin to the public, I want to be sure that on my post's page there are no other meta boxes from other themes and plugins. This is what I searched for and couldn't find any answer to, and so I thought of asking here. Sorry if I was not clear enough before. – AncientRo Mar 16 '17 at 13:34
  • (cont) I realize that this might not even be possible, considering how the WP architecture works, but I do not have enough experience to be sure of this and that is why I asked. – AncientRo Mar 16 '17 at 13:41
  • 1
    @Edit That is a good point. I was thinking about preventing unwanted meta boxes but at the same time, that might also stop proper/valid ones from being available. I will leave things as they are and will not try to force no meta boxes. – AncientRo Mar 16 '17 at 17:20
1

In addition to the above answer. There are globals available for the existing meta boxes. You can also find the latest action/filter hook available on your CPT's (probably do_meta_boxes) and just remove everything.

Global: global $wp_meta_boxes;

Take a look at the remove_meta_box function for how it works: https://developer.wordpress.org/reference/functions/remove_meta_box/

So it will be something like this:

add_action( 'do_meta_boxes', 'my_metabox_hack', 99, 3 );

function my_metabox_hack( $post_type, $cur_context, $post ) {
    global $wp_meta_boxes;

    $screen_id = 'YOUR_SCREEN_ID';
    // Remove anything there
    foreach ( $wp_meta_boxes[$screen_id] as $context => $priorities ) {
        if ( $context !== $cur_context ) {
            continue;
        }
        if ( empty( $priorities ) ) {
            $wp_meta_boxes[$screen_id][$context] = array();
            continue;
        }
        foreach ( $priorities as $priority => $metabox_id ) {
            $wp_meta_boxes[$screen_id][$context][$priority] = array();
        }
    }

    // Now add your own metaboxes bases on the $cur_context parameter.
    // add_meta_box( $id, $title, $callback, $screen, etc.... )
}

Keep in mind that this clobal contains ALL meta boxes, not just for CPT's!

I consider this a hack so please be very carefull when using this approach. It's NOT WP code standards and when not used properly it can break other plugins/functions.

EDIT: I also added an action priority. You can set it even higher to make sure this action is done as last.

EDIT2 Also keep in mind that this action could be fires multiple times and has 3 parameters for each $context. Modified the code for reflect this.

  • 1
    I was in the middle of describing this hack (I've done it once or twice myself) in the edit to my answer, but decided that showing the OP that there are legit reasons for other plugins/themes to add meta boxes to his CPT was better (which he seems to now agree with). – Paul 'Sparrow Hawk' Biron Mar 16 '17 at 17:29
  • Agreed, I just thought to provide the hack in case he realy wanted to. – Jory Hogeveen Mar 16 '17 at 18:49
  • @JoryHovgeveen: BTW, I love View Admin As. Can I email you a question about it? – Paul 'Sparrow Hawk' Biron Mar 16 '17 at 18:59
  • @Paul'SparrowHawk'Biron Thanks! And sure, email / github / slack (@keraweb) is all fine! – Jory Hogeveen Mar 16 '17 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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