4

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

8

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().

1
  • Unsetting global variables seemed like a hacky approach. I'm glad to hear the clean-feeling way is also the right way. Commented May 8, 2013 at 18:08

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.