Avoid Duplicate WordPress Admin Menu Names Using add_menu_page & add_submenu_page

When developing a WordPress plugin, oftentimes it is necessary to create menus and submenus. A frustrating problem that arises is that the menu link created by add_menu_page() is duplicated as the first link of the submenus when the add_submenu_page() function is used with add_menu_page().

In order to avoid duplicate wordpress admin menu names, you will need to create the menus and submenus using the following structure:

add_menu_page( 'Main Menu Item Name', 'Main Menu Item Name', 'manage_options', 'menu_slug', array( $this, 'callback_function_name' ), 'icon_url', 'priority' );

add_submenu_page( 'menu_slug', 'Sub Menu Item Name #1', 'Sub Menu Item Name #1', 'manage_options', 'menu_slug', array( $this, 'callback_function_name_sub_item_1' ) );

add_submenu_page( 'menu_slug', 'Sub Menu Item Name #2', 'Sub Menu Item Name #2', 'manage_options', 'sub_item_2_menu_slug', array( $this, 'callback_function_name_sub_item_2' ) );

add_submenu_page( 'menu_slug', 'Sub Menu Item Name #3', 'Sub Menu Item Name #3', 'manage_options', 'sub_item_3_menu_slug', array( $this, 'callback_function_name_sub_item_3' ) );

In order to avoid the duplicate menu name, we adjust the first submenu to use the menu_slug but with a different name than the main menu. Each submenu then has its own callback function to load the correct page or code.

That’s all you need to do to avoid duplicate admin menu names when using add_menu_page() and add_submenu_page() together!

Leave a Reply

Your email address will not be published. Required fields are marked *