Support » Plugin: WP Custom Menu Filter Plugin » [Plugin: WP Custom Menu Filter Plugin] I have a bug fix regarding Menu IDs
[Plugin: WP Custom Menu Filter Plugin] I have a bug fix regarding Menu IDs
-
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/
-
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; }
Thanks! It works with your solution, so I will change it hoping that it may solve some other difficulties!
You’re welcome! Hope it helps 🙂
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 .
- The topic ‘[Plugin: WP Custom Menu Filter Plugin] I have a bug fix regarding Menu IDs’ is closed to new replies.