Support » Fixing WordPress » Plugin adding and modifying a theme’s existing primary menu items

  • This is both a user experience question and technical one. I’m still fairy new to plugin development but I’m assuming that not all themes will have a ‘primary’ menu. That is the customary menu atop all websites with the obligatory home, about, login, etc.? I suppose in that case if a plugin is wanting to hook into it they need to inform the user that they may need to switch to another theme to enjoy all the plugin’s features?

    And for the technical question, say my plugin creates a ‘Cart’ page with wp_insert_post which seems to automagically create the menu nav item for me – great. How do I find the menu it was added to? The current random theme I’m testing my plugin with, ‘Storefront’, seems to have a primary menu when I run the following:

    
    $menus = get_registered_nav_menus();
    foreach ( $menus as $location => $description ) {
    	echo $location . ': ' . $description;
    }
    

    however, menu_object’s term_id property seems to be null or empty when I run this:

    
    $menu_name = 'primary';
    $locations = get_nav_menu_locations();
    $menu_id = $locations[ $menu_name ] ;
    $menu_object = wp_get_nav_menu_object($menu_id);
    echo $menu_object->term_id;
    

    and has_nav_menu('primary') returns false as well. hmmm..

    So is there a better way of ‘knowing’ if a primary menu exists and then accessing my menu item so that I can update it with wp_update_nav_menu_item?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The location names for menus are completely arbitrary and up to the theme. But you already found the function that tells you what the locations are, and the function that tells you what menus have been created.
    When you create a menu item, does it actually add it to a menu? Do you need to specify the menu id and the parent id (since menus are hierarchical) and the menu order?
    Seems like it would be best if you don’t add it to the menu for the user, since you said you want them to fill out the page. If the page isn’t ready yet, why would the user want it on the menu, and somewhere that they didn’t choose?

    Well, I’m not creating the menu item explicitly. That’s being done indirectly through wp_insert_post’s creation of my Cart page. Now I just need to figure out how to access that menu item it created for me so I can update the Cart count for example displaying: Cart (3)

    And for for the best user experience, I kind of do want to add the menu item as it’s part of the plugin feature-set and I don’t want the user to have to figure out how to wire that up. :/

    I think I’ve uncovered the problem. The theme I was currently testing with, Storefront, has a menu but it’s not visible when you go to Appearance > Menus. The owner of that theme, Woocommerce, must be doing something a bit unorthodox for the creation and display of that menu to work within their own ecosystem and perhaps to prevent 3rd party plugins from directly accessing them.

    No idea, but once I manually created a menu, it overwrote the theme’s menu and I was able to retrieve the ID and then the menu items themselves.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.