jenny-r
12/17/2013 - 9:09 PM

REMOVE ALL THE METABOXES FROM ALL THE THINGS. WARNING: THIS THING IS VERY AGGRESSIVE.

REMOVE ALL THE METABOXES FROM ALL THE THINGS. WARNING: THIS THING IS VERY AGGRESSIVE.

<?php
add_action( 'add_meta_boxes', 'tgm_remove_all_the_metaboxes', 100 );
/**
 * Removes all metaboxes except the ones I WANT ON MY POST TYPE. RAGE.
 *
 * This assumes a specific post type. In my case, it is 'envira'.
 * This function is very aggressive and removes every single metabox minus
 * the needed submitdiv metabox (which allows you to publish something) and
 * the metaboxes that I register with my plugin.
 *
 * Simply edit the variables below to match your needs. :-)
 *
 * IT IS MY POST TYPE. I WANT TO CONTROL ALL THE THINGS.
 *
 * @since 1.0.0
 *
 * @global array $wp_meta_boxes Array of registered metaboxes.
 * @return smile $for_my_buyers Happy customers with no spammy metaboxes!
 */
function tgm_remove_all_the_metaboxes() {

    global $wp_meta_boxes;

    // This is the post type you want to target. Adjust it to match yours.
    $post_type  = 'envira';

    // These are the metabox IDs you want to pass over. They don't have to match exactly. preg_match will be run on them.
    $pass_over  = array( 'submitdiv', 'envira' );

    // All the metabox contexts you want to check.
    $contexts   = array( 'normal', 'advanced', 'side' );

    // All the priorities you want to check.
    $priorities = array( 'high', 'core', 'default', 'low' );

    // Loop through and target each context.
    foreach ( $contexts as $context ) {
        // Now loop through each priority and start the purging process.
        foreach ( $priorities as $priority ) {
            if ( isset( $wp_meta_boxes[$post_type][$context][$priority] ) ) {
                foreach ( (array) $wp_meta_boxes[$post_type][$context][$priority] as $id => $metabox_data ) {
                    // If the metabox ID to pass over matches the ID given, remove it from the array and continue.
                    if ( in_array( $id, $pass_over ) ) {
                        unset( $pass_over[$id] );
                        continue;
                    }

                    // Otherwise, loop through the pass_over IDs and if we have a match, continue.
                    foreach ( $pass_over as $to_pass ) {
                        if ( preg_match( '#^' . $id . '#i', $to_pass ) )
                            continue;
                    }

                    // If we reach this point, remove the metabox completely.
                    unset( $wp_meta_boxes[$post_type][$context][$priority][$id] );
                }
            }
        }
    }

}