I’ve seen two ways to remove meta boxes: remove_meta_box() and unset().

function TEST_remove_dashboard_widgets(){
    remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
}
add_action('wp_dashboard_setup', 'bmc_remove_dashboard_widgets');

vs.

function TEST_remove_dashboard_widgets() {
    global $wp_meta_boxes;
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
}
add_action('wp_dashboard_setup', 'bmc_remove_dashboard_widgets');

remove_meta_box seems better since it’s clearly intended just for that, but I’ve also seen the unset formulation in several places.

Why would I use one vs. the other?

1 Answer
1

When in doubt use the API.

Let’s say the structure of $wp_meta_boxes will change or go away one day.

remove_meta_box() will still work, because the API is a contract between core and developers. Unsetting some keys in a global variable might break.

unset() is easier to write when you want to remove a whole group: unset($wp_meta_boxes['dashboard']) is clearly simpler than running through each separate box. But shorter code is not always better, so this should not be used in public code.

Note, both methods actually work differently: unset() removes an element from the array, while remove_meta_box() set the elements value to FALSE:

foreach ( array('high', 'core', 'default', 'low') as $priority )
    $wp_meta_boxes[$page][$context][$priority][$id] = false;

Other plugins might rely on the existence of that element – and break after you have used unset().

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *