How do I get the name of a menu in WordPress?

You can access the menu metadata using the wp_get_nav_menu_object function BY NAME: $menu = wp_get_nav_menu_object(“my mainmenu” ); BY SLUG: $menu = wp_get_nav_menu_object(“my-mainmenu” ); The return object as follows: Object ( term_id => 4 name => My Menu Name slug => my-menu-name term_group => 0 term_taxonomy_id => 4 taxonomy => nav_menu description => parent => 0 … Read more

Remove a menu item in menu

I think the best way to remove a menu item from a menu is by hooking to wp_nav_menu_objects filter (@Otto answer from the thread you mentioned). And the way you use is first check for the correct menu to filter, and then search for the menu item and remove it by unsetting it from the … Read more

Add Class to Specific Link in Custom Menu

you can use nav_menu_css_class filter add_filter(‘nav_menu_css_class’ , ‘my_nav_special_class’ , 10 , 2); function my_nav_special_class($classes, $item){ if(your condition){ //example: you can check value of $item to decide something… $classes[] = ‘my_class’; } return $classes; } Using this $item you can any condition you want. and this will add the class to the specific li and you … Read more

Error “Trying to get property of non-object” with Custom Walker for wp_nav_menu

I get this error when there are no menus defined or no menus set for the location at Appearance->Menus. When that occurs wp_nav_menu uses a page walker fallback. The fallback (default) for wp_nav_menu is wp_walker_page which uses wp_page_menu which uses wp_list_pages which uses walk_page_tree which uses Walker_Page not Walker_Nav_Menu. And apparently the two walkers are … Read more

How to get current-menu-item title as variable?

This is possible by filtering wp_nav_menu_objects, which is the easiest place to check which item is the current menu item, because WordPress already added the classes for you. add_filter( ‘wp_nav_menu_objects’, ‘wpse16243_wp_nav_menu_objects’ ); function wpse16243_wp_nav_menu_objects( $sorted_menu_items ) { foreach ( $sorted_menu_items as $menu_item ) { if ( $menu_item->current ) { $GLOBALS[‘wpse16243_title’] = $menu_item->title; break; } } … Read more

wp_get_nav_menu_items() not working with slug

Since nobody has really explained why the function doesn’t work the way you thought it did, I shall take a stab at explaining it in detail as I just fell for the same trap as you did. This is where the docs aren’t really clear as to what the slug is referring to. Most people … Read more

Add settings to menu items in the Customizer

You’re running up against an incomplete implementation of modifying nav menus in the Customizer. In particular, inside of the phpdoc for WP_Customize_Nav_Menu_Item_Setting::preview() you can see this comment: // @todo Add get_post_metadata filters for plugins to add their data. Nevertheless, even though we didn’t implemented core support for previewing changes to nav menu item postmeta, it … Read more

Using a menu walker add a custom item at the end of the menu’s items

You don’t need a walker in this case. A filter called wp_nav_menu_items is available. It allows you to edit the list items of a menu. Just append your own list item with search field. add_filter( ‘wp_nav_menu_items’, ‘add_search_to_nav’, 10, 2 ); function add_search_to_nav( $items, $args ) { $items .= ‘<li>SEARCH</li>’; return $items; } Note: if you … Read more

Add a custom walker to a menu created in a widget

If you look at implementation of WP_Nav_Menu_Widget class you will see the following code: function widget($args, $instance) { // Get menu $nav_menu = ! empty( $instance[‘nav_menu’] ) ? wp_get_nav_menu_object( $instance[‘nav_menu’] ) : false; if ( !$nav_menu ) return; $instance[‘title’] = apply_filters( ‘widget_title’, empty( $instance[‘title’] ) ? ” : $instance[‘title’], $instance, $this->id_base ); echo $args[‘before_widget’]; if … Read more