Support

Account

Home Forums ACF PRO Menu Item Depth

Solving

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) {
    
      ob_start();
      echo 'RULE: ';
      print_r($rule);
      echo 'OPTIONS: ';
      print_r($options);
      echo '_POST: ';
      print_r($_POST);
      echo '_GET: ';
      print_r($_GET);
      error_log(ob_get_clean());
      
      return $match;
    }
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.