Home Forums ACF PRO Menu Item Depth


Menu Item Depth

  • Hi,

    I’m using ACF Pro and I’m trying to add a custom field to some menu items, however I only want to add it to items of a particular depth. I’ve started writing a plugin for it however I can’t figure out how to access the menu item info from the rule_match filter. Is this possible?

    Should look something like this only “nav_menu_item” doesn’t contain an array, it’s just a string that tells you what type of menu item you are dealing with e.g. “post_type”, “taxonomy”, “custom” so that bit is pseudocode.

    add_filter('acf/location/rule_match/menu_item_depth', 'acf_menu_depth_location_rules_match_taxonomy_depth', 10, 3);
    function acf_menu_depth_location_rules_match_taxonomy_depth($match, $rule, $options) {
      if($options['nav_menu_item']['depth'] === 1) { // Pseudocode
        $match = true;
      return $match;

    I know that there is a location/screen filter to add extra info to the $options parameter above but again I can’t figure out how to access the menu item from here (or if it’s even possible)

    add_filter('acf/location/screen', 'acf_menu_depth_location_screen_options', 10, 2);
    function acf_menu_depth_location_screen_options($options, $field_group) {
      $options['depth'] = $field_group['nav_menu_item']['depth']; // Again pseudocode
      return $options;
  • I’m also trying to achieve this. Where you able to solve it in the meantime?

  • I don’t think I did, honestly it’s so long ago now I can’t really remember but I just had a look at the relevant project and can’t see any code relating to it.

    I believe that the menu items have a class menu-item-depth-0, menu-item-depth-1, etc. So you could add some custom CSS to the WordPress admin that hides the extra fields then shows them at a specific depth however this is a bit of a hack as the fields will still exist and will still save values you just won’t see them when editing.

  • I have not tried creating location rules for menus. My guess is that you’ll need to look at the information that ACF passes to the filter and then probably do some queries about the menu item in question to see if it is a sub menu. However, I don’t know how to do this. All I can do is give you some advice on how to see what information you have to work with. Basically, you can log information to your error log so that you can see what you have to work with.

    add_filter('acf/location/rule_match/menu_item_depth', 'acf_menu_depth_location_rules_match_taxonomy_depth', 10, 3);
    function acf_menu_depth_location_rules_match_taxonomy_depth($match, $rule, $options) {
      echo 'RULE: ';
      echo 'OPTIONS: ';
      echo '_POST: ';
      echo '_GET: ';
      return $match;
  • Just curious if anyone ever figured this out. I’m assuming it would be much more difficult because of the menu UI and the ability to drag items to change the level. I’d like to do this but I do not have the hours available to work it out.

  • I got halfway there. With the help of this thread and some other research, I managed to find that the $options parameters of the rule_match callback has the following data:

    array(5) { 
        ["lang"]=> string(0) "" 
        ["ajax"]=> bool(false) 
        ["nav_menu_item"]=> string(9) "post_type" 
        ["nav_menu_item_id"]=> string(3) "395" 
        ["nav_menu_item_depth"]=> int(1) 

    Using the nav_menu_item_depth I was able to show the field only on the related depth.

    Unfortunately this does not update when drag and dropping menu elements around. Adding that functionality would require dabbing in the related JavaScript and I’m not even sure that’s possible.

    Edit: code formatting and punctuation.

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Menu Item Depth’ is closed to new replies.