1

I am trying to make a basic plugin and want to add or remove sub menu item on plugin activation or deactivation. I am trying to achieve this with wp_get_nav_menu_items()

 $menuLocations = get_nav_menu_locations(); $menuID = $menuLocations['primary']; $primaryNav = wp_get_nav_menu_items($menuID); 

I have three menus Home About product. I want to add a submenu under product on plugin activation.

3
  • Custom menus and submenus are not saved in database but set at every page call. To create them, look theses 2 functions : developer.wordpress.org/reference/functions/add_menu_page developer.wordpress.org/reference/functions/add_submenu_page Commented Oct 20, 2017 at 12:12
  • @mmm question is about navigation menus, not admin menu. :) Commented Oct 20, 2017 at 12:49
  • oh ! sorry. for navigation menus, I saw the function wp_nav_menu_update_menu_items but there is no documentation then it can be more difficult to do that (but not impossible) Commented Oct 20, 2017 at 13:12

1 Answer 1

0

If you want to add items dynamically at every page call, you can look this answer :
Dynamically add sub-categories to any category in the menu

And to add an element directly, you can try that for a custom link e.g. :

// create the new element $post = [ 'post_title' => "StackExchange " . date("H:i:s"), "menu_order" => 700, // position in the menu 'post_type' => 'nav_menu_item', 'post_status' => 'publish', ]; $menu_item_db_id = wp_insert_post( $post ); update_post_meta( $menu_item_db_id, 'url', "https://wordpress.stackexchange.com/"); update_post_meta( $menu_item_db_id, 'type', "custom"); update_post_meta( $menu_item_db_id, '_menu_item_menu_item_parent', 0); // change to create a sub menu // association to the menu $menuLocations = get_nav_menu_locations(); $menuID = (int) $menuLocations['primary']; wp_set_object_terms($menu_item_db_id, $menuID, "nav_menu"); 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.