5

Is it possible to limit maximum menu depth in admin panel /nav-menus.php?

I need this because my theme won't support more than two levels of menu, so there is no point in allowing a user to make deeper menus.

11

The solution that I came up with:

/**
 * Limit max menu depth in admin panel to 2
 */
function q242068_limit_depth( $hook ) {
  if ( $hook != 'nav-menus.php' ) return;

  // override default value right after 'nav-menu' JS
  wp_add_inline_script( 'nav-menu', 'wpNavMenu.options.globalMaxDepth = 1;', 'after' );
}
add_action( 'admin_enqueue_scripts', 'q242068_limit_depth' );
| improve this answer | |
1

Follow up on @jmarceli's great answer to target specific menu locations

function squarecandy_menu_admin_limit_depth( $hook ) {
  if ( 'nav-menus.php' !== $hook ) {
    return;
  }

  wp_enqueue_script( 'squarecandy-admin-menu-depth', get_stylesheet_directory() . '/js/admin-menu-depth.js', array(), '1.0.0', true );
}
add_action( 'admin_enqueue_scripts', 'squarecandy_menu_admin_limit_depth' );

js

// Force Menu Depth to Match Front End

// main menu can only have one child level
if ( document.getElementById("locations-main-menu").checked ) {
    wpNavMenu.options.globalMaxDepth = 1;
}

// footer menu have only top level
if ( document.getElementById("locations-footer-menu").checked ) {
    wpNavMenu.options.globalMaxDepth = 0;
}

// all other menu locations stay at the WP default
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.