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
?
- The topic ‘Plugin adding and modifying a theme’s existing primary menu items’ is closed to new replies.