WordPress.org

Forums

WP Custom Menu Filter Plugin
[resolved] I have a bug fix regarding Menu IDs (5 posts)

  1. sevenspark
    Member
    Posted 3 years ago #

    Hey,

    Nice plugin, but I found some faulty logic regarding Menu HTML IDs versus Menu Object IDs. In wps_custom_nav_menu_items() (line 188), the first check is for:

    if ( $args['menu_id'] ){
      $menu_id = $args['menu_id'];
    }

    However, the menu_id argument is the string for the ID attribute of the menu's top-level UL element. If not set in wp_nav_menu, I believe it defaults to the menu object's (database) ID. However, this is not a reliable way to determine the menu object's ID, which is what is required, since it can easily be set in the wp_nav_menu() call (as in my case). If that occurs, the ID is not properly determined and the remaining plugin logic fails.

    The second bit of logic is a much better solution:

    if ( $args['theme_location'] ) {
      $menu_locations = get_nav_menu_locations();
      $menu_id = $menu_locations[$args['theme_location']];
    }

    because if the theme_location parameter is set (as it almost always should be), this is a reliable way to determine the menu object ID.

    I'm not convinced that the $args['menu_id'] check should be there at all, unless it is accompanied by an is_numeric check (which still has the potential to be unreliable), but I propose this rearranged logic:

    // Get menu id
    //Check the theme location first
    if ( $args['theme_location'] ) {
      $menu_locations = get_nav_menu_locations();
      $menu_id = $menu_locations[$args['theme_location']];
    }
    //Try the menu ID, if it's a number
    else if ( isset( $args['menu_id'] )  && is_numeric( $args['menu_id'] ) ){
      $menu_id = $args['menu_id'];
    }
    else {
      $nav_item_db_id = $sorted_menu_items[1]->ID;
      $nav_menu = wp_get_object_terms( $nav_item_db_id, 'nav_menu' );
      $menu_id = $nav_menu[0]->term_id;
    }

    Hope that helps! :)

    Chris

    http://wordpress.org/extend/plugins/wp-custom-menu-filter-plugin/

  2. sevenspark
    Member
    Posted 3 years ago #

    Thought about it a little more, and I don't think the menu_id arg is ever going to to supply the appropriate menu object ID. However, it could be replaced with an is_numeric check for the 'menu' parameter - or a more complicated retrieved process for the menu ID based on the menu slug or name from the 'menu' arg.

    //Check the theme location first
    if ( $args['theme_location'] ) {
      $menu_locations = get_nav_menu_locations();
      $menu_id = $menu_locations[$args['theme_location']];
    }
    //Try the menu ID, if it's a number
    else if ( isset( $args['menu'] )  && is_numeric( $args['menu'] ) ){
      $menu_id = $args['menu'];
      //alternatively, allow non-numeric values and look up menu ID by slug
    }
    else {
      $nav_item_db_id = $sorted_menu_items[1]->ID;
      $nav_menu = wp_get_object_terms( $nav_item_db_id, 'nav_menu' );
      $menu_id = $nav_menu[0]->term_id;
    }
  3. Travis Smith
    Member
    Plugin Author

    Posted 2 years ago #

    Thanks! It works with your solution, so I will change it hoping that it may solve some other difficulties!

  4. sevenspark
    Member
    Posted 2 years ago #

    You're welcome! Hope it helps :)

  5. Siddharth
    Member
    Posted 2 years ago #

    Hello Sevenpark.
    i purchase the ubermenu.
    but the search bar shortcode [ubermenu-search] not working.

    on the right side instead of search bar the shortcode is showing.
    can you please suggest .

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WP Custom Menu Filter Plugin
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic