WC_Admin_Menus::filter_default_nav_menu_hidden_meta_boxes
Filter the default hidden meta boxes for the Appearance > Menus screen.
Only applies when the user has no saved preference for the nav-menus screen. Returns a list of every registered nav-menus meta box except the four core visible boxes, the "WooCommerce endpoints" box, and the WC taxonomy boxes. Product Brands is visible only when its taxonomy exists.
Method of the class: WC_Admin_Menus{}
No Hooks.
Returns
mixed. The filtered option value.
Usage
$WC_Admin_Menus = new WC_Admin_Menus(); $WC_Admin_Menus->filter_default_nav_menu_hidden_meta_boxes( $result, $option, $user );
- $result(mixed) (required)
- Value for the user's option, or false if not set.
- $option(string) (required)
- Name of the option being retrieved.
- $user(WP_User) (required)
- WP_User object of the user whose option is being retrieved.
Changelog
| Since 11.1.0 | Introduced. |
WC_Admin_Menus::filter_default_nav_menu_hidden_meta_boxes() WC Admin Menus::filter default nav menu hidden meta boxes code WC 11.1.2
public function filter_default_nav_menu_hidden_meta_boxes( $result, $option, $user ) {
global $wp_meta_boxes;
// Once a snapshot exists the option reads non-false, so this early return
// also makes the update_user_meta() call below idempotent: a re-entrant read
// bails here instead of persisting again.
if ( false !== $result ) {
return $result;
}
if ( ! $user || ! isset( $wp_meta_boxes['nav-menus'] ) || ! is_array( $wp_meta_boxes['nav-menus'] ) ) {
return $result;
}
$visible = array(
'add-post-type-page',
'add-post-type-post',
'add-custom-links',
'add-category',
'add-product_cat',
'add-product_tag',
'woocommerce_endpoints_nav_link',
);
if ( taxonomy_exists( 'product_brand' ) ) {
$visible[] = 'add-product_brand';
}
$hidden = array();
foreach ( $wp_meta_boxes['nav-menus'] as $priorities ) {
foreach ( (array) $priorities as $priority => $boxes ) {
foreach ( (array) $boxes as $box ) {
if ( isset( $box['id'] ) && ! in_array( $box['id'], $visible, true ) ) {
$hidden[] = $box['id'];
}
}
}
}
// Persist the snapshot, mirroring core's wp_initial_nav_menu_meta_boxes(),
// so later reads in this request and in future requests return this same
// list instead of recomputing it against a changed meta box registry.
//
// Like core, only the current user's snapshot is persisted. This filter
// runs for whichever user's option is being read, so a read performed on
// behalf of another user (user-switching or admin tooling) still receives
// the computed default below for display, but must not consume that user's
// durable first-visit snapshot from this request's meta box registry.
$current_user_id = get_current_user_id();
if ( $current_user_id && (int) $user->ID === $current_user_id ) {
update_user_meta( $current_user_id, 'metaboxhidden_nav-menus', $hidden );
}
return $hidden;
}