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?
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, whileremove_meta_box()
set the elements value toFALSE
:Other plugins might rely on the existence of that element – and break after you have used
unset()
.